一、
爬虫:代码模拟浏览器向服务器发送请求并得到目标数据。
二、
1.get请求
1 import requests 2 3 query = input("请输入你想查询的内容:") 4 url = f"https://www.sogou.com/web?query={query}" 5 6 header = { 7 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36" 8 } 9 10 res = requests.get(url, headers=header) 11 12 print(res) 13 print(res.request) 14 print(res.encoding) 15 16 with open('sougou.html', mode="w", encoding='utf-8') as f: 17 f.write(res.text) 18 f.close()
2.post请求
import requests url = "https://fanyi.baidu.com/sug" s = input('请输入您想查询的单词:') da = { 'kw': s } res = requests.post(url, data=da) res.close() res = res.json() print(res['data'][0]['k']+'的中文翻译为为:') print(res['data'][0]['v'])