1.导入requests模块
2.User-agent伪装(ua伪装)
3.代码实现
# 爬取百度首页页面
import requests
url = "http://www.baidu.com"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
}
resp = requests.get(url,headers=headers)
with open("my_baidu.html", "w", encoding="utf-8") as fp:
fp.write(resp.text)
print("ok")