百度翻译数据采集
import urllib.request
import urllib.parse
import json
def gethtml(url):
# 伪装浏览器
headers = {‘User-Agent’:‘Mozilla/5.0 (Windows NT 6.1; WOW64)’}
r = urllib.request.Request(url, headers=headers)
# 定义post请求的参数
name = input(“请输入您要翻译的英文:”)
formdata = {
‘kw’:name
}
# 解析post参数
query_string = urllib.parse.urlencode(formdata).encode()
# 向服务器发送请求
response = urllib.request.urlopen(r,data=query_string)
# 读取网页内容
html = response.read().decode()
a = json.loads(html)
#print(html)
print(a)
gethtml(“https://fanyi.baidu.com/sug”)