python 爬虫requests 发送post请求 实现百度翻译返回结果
实列说明
python 爬虫requests 发送post请求 ,实现百度翻译返回结果
实列代码:
# 代码说明:requests 发送post数据到百度翻译,返回结果 import requests # 定义url url = 'https://fanyi.baidu.com/sug' # 定义请求头信息 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0' } # post发送请求数据 data = { 'kw': '我爱你' } res = requests.post(url=url, headers=headers, data=data) # 获取相应状态码 code = res.status_code # 相应成功后把相应的内容写入文件中 if code == 200: # 返回json结果 # {'errno': 0, 'data': [{'k': '我爱你', 'v': '名. I love you; <法>Je t’aime; <德>Ich liebe Dich; <日'}, {'k': '我爱你一生一世', 'v': 'I love you forever'}]} print(res.json()['data'][0]['k'])