Python Crawler Tutorial -08-post introduction (Next)
In order to set up request information more, simply through the Urlopen has not been able to meet the requirements, at this time need to use request. Request Class
Constructing a Request instance
req = request.Request(url=baseurl,data=data,headers=header)
Make a request
rsp = request.urlopen(req)
File:
Case V8 File: https://xpwi.github.io/py/py%E7%88%AC%E8%99%AB/py08post2.py
# 案例v7百度翻译# 使用Requestfrom urllib import request,parseimport jsonbaseurl = ‘http://fanyi.baidu.com/sug‘keyword = input("请输入需要翻译的内容:")data = { ‘kw‘: keyword}# 需要使用parse模块对data进行编码data = parse.urlencode(data)data = data.encode(‘utf-8‘)header = { ‘Content-Length‘:len(data)}# 构造Request实例req = request.Request(url=baseurl,data=data,headers=header)# 发出请求rsp = request.urlopen(req)json_data = rsp.read().decode()# 把json字符串转换为字典json_data = json.loads(json_data)for item in json_data[‘data‘]: # if item[‘k‘] == keyword: print(item[‘k‘], ": ", item[‘v‘])
Bye
-This note does not allow any person or organization to reprint
Python Crawler Tutorial -08-post Introduction (Baidu translation) (next)