四、2021-10-19//Requests库新增实例(课堂笔记)
一、UA伪装
import requests
#UA伪装
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
#1.确定URL
url = 'https://www.sogou.com/web'
#2.输入参数
keyword = input('请输入一个关键字:')
param = {'query':keyword}
#3.发起请求
r=requests.get(url,params = param,headers=headers)
#4.输出/持久化存储
page_txt = r.text
filename = keyword+'.html'
with open(filename,'w',encoding='utf-8') as fp:
fp.write(page_txt)
print(filename,'saved!')
二、json
import requests
import json
#UA伪装
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
#1.确定URL
url = 'https://fanyi.baidu.com/sug'
#2.输入参数
keyword = input('请输入一个关键字:')
param = {'kw':keyword}
#3.发起请求
r=requests.get(url,params = param,headers=headers)
#4.输出/持久化存储
dict_obj = r.json()
filename = keyword + '.json'
fp = open(filename,'w',encoding='utf-8')
json.dump(dict_obj,fp,ensure_ascii=False)
fp.close()
print(filename,'saved!')