您的位置:首页 > 编程语言 > Python开发

urllib通过Post请求爬去数据并解析JSON(Python)

2018-03-20 17:13 1026 查看
1.   封装的请求post函数:def downloadPostPage(url, dictdata, headers, charset='utf-8', reqnum=5):
data = bytes(parse.urlencode(dictdata), encoding=charset)
req = request.Request(url, data, headers=headers,method='POST')
info= None
try:
response = request.urlopen(req)
info = response.read().decode(charset)
except error.HTTPError as e:
# 服务器错误
print(e.code)
if reqnum > 0:
if hasattr(e, 'code') and 500 <= e.code <= 600:
time.sleep(random.randint(5, 11))
downloadPostPage(url,dictdata,headers, charset=charset, reqnum=reqnum-1)
except error.URLError:
print("url error")
return info2.   主函数的实现:if __name__ == '__main__':
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
}
while True:
key = input('输入搜索词:')
if not key:
break
dictdata = {
"i": key,
"from": "auto",
"to": "auto",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": "1511219405946",
"sign": "f8965f67a1d3eee8a69ddf8ccc5f582b",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTIME",
"typoResult": "false"
}
info = downloadPostPage(url, dictdata, headers=headers,reqnum=1)
jsonLoads = json.loads(info)
print(jsonLoads)
print(jsonLoads['translateResult'][0][0]['tgt'])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: