使用爬虫破解百度翻译【★★】

By yesmore on 2021-07-23
阅读时间 1 分钟
文章共 200
阅读量

携带post参数的爬虫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import requests
import json

if __name__ == "__main__":
# 1.指定url
post_url = 'https://fanyi.baidu.com/sug'
# 2.进行UA伪装
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
# 3.post请求参数处理(同get请求一致)
print('******************************************************')
word = input('请输入您要翻译的文本: ')
data = {
'kw': word # 待翻译数据
}
# 4.请求发送
response = requests.post(url=post_url, data=data, headers=headers)
# 5.获取响应数据:json()方法返回的是obj(如果确认响应数据是json类型的,才可以使用json())
dic_obj = response.json()
print(dic_obj)
# 持久化存储
fileName = word + '.json'
fp = open(fileName, 'w', encoding='utf-8')
json.dump(dic_obj, fp=fp, ensure_ascii=False)

print('******************************************************')

Tips: Please indicate the source and original author when reprinting or quoting this article.