以下是使用Python的Requests库编写一个使用代理读取domain.txt内的网址,请求目标网址提取Title并将结果存入txt档的代码:

import requests

# 代理地址和端口号
proxy = {
    'http': 'http://yourproxyaddress:port',
    'https': 'https://yourproxyaddress:port'
}

# 读取domain.txt文件
with open('domain.txt', 'r') as file:
    lines = file.readlines()

# 遍历每一行网址进行请求
for line in lines:
    url = line.strip()  # 去除行尾的换行符
    try:
        # 使用代理请求网址获取页面内容
        response = requests.get(url, proxies=proxy)

        # 提取页面Title
        title = response.text.split('<title>')[1].split('</title>')[0]

        # 将结果写入txt文件
        with open('result.txt', 'a') as file:
            file.write(f'{url}: {title}\n')
    except Exception as e:
        print(f'{url}请求失败:{e}')

解释:

  1. 首先定义了一个代理字典,包含http和https两种协议的代理地址和端口号。
  2. 使用with open('domain.txt', 'r') as file打开domain.txt文件,并使用readlines()方法读取每一行网址。
  3. 使用for line in lines遍历每一行网址,并使用strip()方法去除行尾的换行符。
  4. 使用try...except语句,对每一行网址进行请求,并使用代理字典中的代理。如果请求成功,则提取页面Title,并使用with open('result.txt', 'a') as file打开result.txt文件,并使用write()方法将结果写入文件。
  5. 如果请求失败,则在控制台输出错误信息
使用python使用requests库编写一个使用代理读取domaintxt内的网址请求目标网址提取Title并将结果存入txt档

原文地址: https://www.cveoy.top/t/topic/fjsP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录