import requests
if __name__=='__main__':
headers={
#UA伪装,下面有介绍怎么获取到User-Agent
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}
url='https://www.sogou.com/web'
kw=input('enter a woed:')
#把参数封装到字典
param={
'query':kw
}
#对指定的url发起请求
respo = requests.get(url=url,params=param,headers=headers)
pespo_text=respo.text
filename=kw+".html"
#持久化保存,生成一个HTML文件
with open(filename,'w',encoding='utf-8') as fp:
fp.write(pespo_text)
print(filename,'保存成功')
首先F12进入开发者模式
然后选择Network
选择logo115_50.png?v=5 选择Headers,找到User-Agent复制到代码中