ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
哪些地方会用到POST请求? 1. 登录注册(POST比GET安全) 2. 需要传输大文本的时候(POST请求对数据长度没有要求) 爬虫也需要在这两个地方模拟浏览器发送POST请求。 ```python # 用法 response = requests.post('http://www.baidu.com', data=data, headers=headers) ``` <br/> **案例:百度翻译** ![](https://img.kancloud.cn/5d/9c/5d9cd427fad2e56947e575efa07bc29b_780x392.png) ![](https://img.kancloud.cn/1b/d2/1bd2a722fa3ddd46430edb6549dcf32a_970x386.png) (1)PC版 ```python import requests headers={'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36' } data={ 'from': 'zh' 'to': 'en' 'query': '爬虫' 'transtype': 'translang' 'simple_means_flag': '3' 'sign': '253813.474180' 'token': '12c2cd4f509e3b812cddee47862ae4e2' 'domain': 'common' } post_url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en' response = requests.post(post_url, data=data, headers=headers) print(response.text) ``` 这个程序是爬取不到任何数据的,因为百度翻译根据sign进行了反爬加密,在这里并没有破解。 (2)手机版 ```python import requests headers={'User-Agent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1' } data={ 'query':'爬虫' 'from':'zh' 'to':' en' 'token':'12c2cd4f509e3b812cddee47862ae4e2' 'sign':'253813.474180' } post_url = 'https://fanyi.baidu.com/basetrans' response = requests.post(post_url, data=data, headers=headers) print(response.text) ```