Python Crawler Tutorial -08-post Introduction (Baidu translation) (next)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.