数据分析训练营-urllib实战与反爬策略-request对象之post请求案例分析-百度翻译
程序员文章站
2022-07-05 19:54:45
import urllib.request as urimport urllib.parse as upimport jsonword = input('请输入要翻译的中文')data = { 'kw':word}data_url = up.urlencode(data)#data转义成request请求能读取的datarequest = ur.Request( url='https://fanyi.baidu.com/sug', data=data_url.e...
import urllib.request as ur
import urllib.parse as up
import json
word = input('请输入要翻译的中文')
data = {
'kw':word
}
data_url = up.urlencode(data)#data转义成url编码
request = ur.Request(
url='https://fanyi.baidu.com/sug',
data=data_url.encode('utf-8'),#data_url转成字节
)
response = ur.urlopen(request).read()#收到返回的结果,是json对象
# print(response)
ret = json.loads(response)#json对象转换到python对象
# print(ret)
translate = ret['data'][0]['v']
print(translate)
步骤1 响应的是json类型
分析url
本文地址:https://blog.csdn.net/lildn/article/details/110938512