使用python语言应用requests库读取domaintxt内网址列表在百度搜索内进行搜索最近收录结果标题并存入resulttxt
以下是一个可能的解决方案:
import requests
# 读取网址列表
with open('domain.txt') as f:
domains = [line.strip() for line in f]
# 在百度搜索内搜索并保存结果
with open('result.txt', 'w') as f:
for domain in domains:
url = f'https://www.baidu.com/s?q1={domain}&rn=50'
response = requests.get(url)
response.raise_for_status() # 检查是否有错误发生
for i, result in enumerate(response.json()['feed']['entry']):
title = result['title']
f.write(f'{domain} - {i+1}: {title}\n')
解释:
- 读取
domain.txt文件,将每行的网址存入domains列表中。 - 对于每个网址,构建百度搜索的URL,并使用
requests.get函数发起GET请求。 - 检查响应是否有错误发生,并使用
response.json()方法将响应的JSON数据转换为Python对象。 - 遍历搜索结果的条目,并提取标题。将结果写入
result.txt文件
原文地址: https://www.cveoy.top/t/topic/fjvB 著作权归作者所有。请勿转载和采集!