pycharm利用request请求百度搜索关键词并读取源码下载到本地


import
requests params = { 'wd' : 'seo' } headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' } response = requests.get("http://www.baidu.com/s",params=params,headers=headers) with open('baidu.html','w',encoding='utf-8') as fp: fp.write(response.content.decode('utf-8')) print(response.url)

执行结果如下:

下载下来的baidu.html文件

 

posted @ 2019-07-14 22:27  secsafe  阅读(368)  评论(0编辑  收藏  举报