例如对搜狗浏览器搜索“画家”实现如下:
import requests
URL = "https://www.sogou.com/web"
#UA伪装
header = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67'
}
#参数
param = {
'query':'画家'
}
#发起请求
response = requests.get(url = URL,headers = header,params = param)
#获取响应数据
page_ = response.text
#保存文件
with open('画家.html','w',encoding = 'utf-8') as fp:
fp.write(page_)
print("保存成功")
效果如下