废话不多说,直接上代码
import requests def main(): url='https://www.sogou.com/web' headers={ 'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0' } kw=input('输入要查找的关键词:') #封装参数 param={ 'query':kw } response=requests.get(url=url,params=param,headers=headers) page_text=response.text filename=kw+'.html' with open(filename,'w',encoding='utf-8') as fp: fp.write(page_text) print(filename+'保存成功') if __name__=='__main__': main()
下面是搜索到一些内容的部分截图: