#UA:User-Agent(请求载体身份标识)
import requests
if __name__ == '__main__':
#UA伪装:将对应的User-Agent封装到一个字典中
header ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
#指定url
url="https://www.sogou.com/web"
#处理url携带的参数:封装到字典中 (设置成动态的)
keyword = input('enter a word:')
param ={
'query':keyword
}
#对指定的url发起的请求对应的url是携带参数的,并且请求过程中处理了参数
response = requests.get(url=url,params=param,headers=header)
#将获取的结果转为字符串形式
page_text = response.text
#保存于文件当中
filename = keyword+'.html'
with open(filename,'w',encoding='utf-8') as fp:
fp.write(page_text)
print(filename,'保存成功!!!')