使用python语言应用requests库读取domaintxt内网址列表在百度搜索内用site网址进行搜索使用beautiful soup将最近收录结果标题并存入resulttxt
以下是使用Python语言应用requests库、beautiful soup库实现读取domain.txt内网址列表,在百度搜索内用site:网址进行搜索,并将最近收录结果标题存入result.txt的代码:
import requests
from bs4 import BeautifulSoup
# 读取domain.txt文件中的网址列表
with open('domain.txt', 'r') as f:
domains = f.read().splitlines()
# 搜索结果存放的文件
result_file = open('result.txt', 'w')
# 遍历每个网址进行搜索
for domain in domains:
# 构造搜索url
search_url = 'https://www.baidu.com/s?wd=site%3A' + domain
# 发送请求并获取响应
response = requests.get(search_url)
# 解析响应内容,获取搜索结果列表
soup = BeautifulSoup(response.content, 'html.parser')
results = soup.find_all('div', class_='result')
# 遍历搜索结果列表,获取最近收录结果的标题并存入文件
for result in results:
title = result.find('h3').get_text()
result_file.write(title + '\n')
# 关闭文件
result_file.close()
在以上代码中,首先使用open()函数读取domain.txt文件中的网址列表,并使用splitlines()方法将其分割成一个列表。然后,使用requests库发送请求,构造搜索url并获取响应内容。接着,使用beautiful soup库解析响应内容,获取搜索结果列表。最后,遍历搜索结果列表,获取最近收录结果的标题,并将其存入result.txt文件中。最后,关闭文件。
注意:在使用以上代码前,请先安装requests库和beautiful soup库。可使用以下命令进行安装:
pip install requests
pip install beautifulsoup4
``
原文地址: https://www.cveoy.top/t/topic/fjwp 著作权归作者所有。请勿转载和采集!