request模块
1、基本操作
'''
- 指定url
- 发起请求
- 获取响应的数据
- 持久化存储
'''
import requests
if __name__ == "__main__":
url = "https://www.sogou.com/"
response = requests.get(url=url)
page_text = response.text
print(page_text)
with open("./sougou.html", "w", encoding="UTF-8") as fp:
fp.write(page_text)
2、UA伪装
import requests
if __name__ == "__main__":
url = "https://www.sogou.com/web"
kw = input('enter a word:')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
}
param = {
'query': kw
}
proxies = {"HTTP":"http://123.169.122.201:9999"}
response = requests.get(url=url, params=param, headers=headers, proxies=proxies)
page_text = response.text
with open("./sougou.html", "w", encoding="UTF-8") as fp:
fp.write(page_text)
3、小案例
'''
- poat请求(携带了参数)
- 相应的数据是一组json数据
json.load('json数据')可以变成字符串形式
'''
import requests
import json
if __name__ == "__main__":
post_url = "https://fanyi.baidu.com/sug"
data = {
'kw': 'dog'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
}
response = requests.post(url=post_url, data=data, headers=headers)
dic_obj = post_text = response.json()
with open('./dog.json', 'w', encoding='UTF-8') as fp:
json.dump(dic_obj, fp=fp, ensure_ascii=False, indent=2)
|