python爬取百度搜索,参考代码Ai学的代码,几秒钟就写完了,贼快。代码在最后面
注意:需要安装bs4模块
pip install beautifulsoup4
如果你的环境中同时安装了Python 2和Python 3,可能需要使用pip3来安装:
pip3 install beautifulsoup4
代码如下。
import requests
from bs4 import BeautifulSoup
def search_baidu(keyword, page):
url = f"https://www.baidu.com/s"
params = {
"wd": keyword,
"pn": (page-1) * 10, # 根据页数计算偏移量
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36",
}
response = requests.get(url, params=params, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
results = soup.find_all("h3", class_="t")
for result in results:
link = result.a["href"]
title = result.a.get_text()
print(title)
print(link)
print("-" * 50)
# 搜索关键词和页数
keyword = "Python"
page = 1
search_baidu(keyword, page)