python搜索关键词自动提交
时间: 2023-07-26 17:04:11 浏览: 94
可以使用Python的requests库和BeautifulSoup库实现自动提交关键词搜索。首先,使用requests库模拟浏览器登录搜索引擎网站,并提交搜索请求。然后,使用BeautifulSoup库解析返回的HTML页面,提取搜索结果。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 模拟浏览器登录搜索引擎网站
url = 'http://www.baidu.com/s'
keyword = 'python'
params = {'wd': keyword}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, params=params, headers=headers)
# 解析HTML页面,提取搜索结果
soup = BeautifulSoup(response.text, 'html.parser')
results = soup.find_all('div', class_='result')
for result in results:
title = result.h3.a.text.strip()
link = result.h3.a['href'].strip()
print(title)
print(link)
```
这个示例代码使用requests库模拟了浏览器登录百度搜索,搜索关键词为'python'。然后,使用BeautifulSoup库解析返回的HTML页面,提取搜索结果并打印出来。