以下是使用Python语言应用requests库,读取domain.txt内网址并检测是否包含http://若无则添加,使用requests库对读取出来的网址应用随机user agent及代理取得网页响应的标题并存入results.txt内的代码:

import requests
from random import choice

# 读取domain.txt内网址
with open('domain.txt', 'r') as f:
    domains = f.read().splitlines()

# 检测并添加http://
urls = []
for domain in domains:
    if not domain.startswith('http'):
        url = 'http://' + domain
    else:
        url = domain
    urls.append(url)

# 定义user agents列表
user_agents = [
    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko',
    'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; AS; rv:11.0) like Gecko',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
    'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'
]

# 定义proxies列表
proxies = [
    {'http': 'http://218.92.227.162:8080', 'https': 'http://218.92.227.162:8080'},
    {'http': 'http://58.218.92.116:8080', 'https': 'http://58.218.92.116:8080'},
    {'http': 'http://118.190.95.35:9001', 'https': 'http://118.190.95.35:9001'},
    {'http': 'http://124.89.33.73:9999', 'https': 'http://124.89.33.73:9999'},
    {'http': 'http://117.88.4.131:3000', 'https': 'http://117.88.4.131:3000'}
]

# 遍历urls列表,应用随机user agent及代理取得网页响应的标题并存入results.txt内
with open('results.txt', 'w', encoding='utf-8') as f:
    for url in urls:
        try:
            # 随机选择user agent及代理
            headers = {'User-Agent': choice(user_agents)}
            proxy = choice(proxies)
            # 发送请求
            response = requests.get(url, headers=headers, proxies=proxy, timeout=5)
            # 提取标题
            title = response.text.split('<title>')[1].split('</title>')[0]
            # 写入结果到文件中
            f.write(url + '\t' + title + '\n')
            print(url + '\t' + title)
        except:
            print(url + '\t请求失败')
``
使用python语言应用requests库读取domaintxt内网址并检测是否包含http若无则添加使用requests库对读取出来的网址应用随机user agent及代理取得网页响应的标题并存入resultstxt内

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

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