Python 搜狗关键词查询代码 - 自定义链接搜索
以下是用Python实现的搜狗关键词查询代码示例,可以检索搜索结果中自定义设置的搜索链接。需要安装requests、BeautifulSoup4库。
import requests
from bs4 import BeautifulSoup
# 搜狗搜索链接
url = 'https://www.sogou.com/web'
# 自定义设置的搜索链接
custom_link = 'https://www.example.com'
# 搜索关键词
keyword = 'Python'
# 构造搜索请求参数
params = {
'query': keyword,
'ie': 'utf8',
'page': 1
}
# 发送搜索请求并获取响应内容
response = requests.get(url, params=params)
html = response.text
# 解析搜索结果页面
soup = BeautifulSoup(html, 'html.parser')
results = soup.find_all('div', class_='vrwrap')
# 遍历搜索结果,查找自定义链接
for result in results:
link = result.find('a')['href']
if custom_link in link:
# 打印匹配的搜索结果标题和链接
title = result.find('h3').get_text()
print(title)
print(link)
运行上述代码后,会输出所有搜索结果中包含自定义链接的标题和链接。如果没有匹配的结果,则不会有输出。
原文地址: https://www.cveoy.top/t/topic/m1HW 著作权归作者所有。请勿转载和采集!