The __python of Python Learning

Source: Internet
Author: User

Click to enter Requests website
I. Installation of requests
1.$ PIP Install requests
If an error occurs: OSError: [Errno] Permission denied: '/library/python/2.7/site-packages/requests '
You can add sudo before the PIP, which is $ sudo pip install requests.
2.$ Easy_install requests, this method is not recommended for use.

Two. Compare URLLIB2 module and requests module (get "http://www.baidu.com" source)
1. Use of URLLIB2

Import urllib2
>>> url= ' http://www.baidu.com '
>>> response=urllib2.urlopen (URL)
>>> response.getcode
>>> response.headers.getheader (' content-type ')
' text/ html Charset=utf-8 '
>>> response.read ()
' <! DOCTYPE html><!--STATUS ok-->

Explanation: Urllib2 Call method to read property information in response
2. Use of Requests

>>> Import Requests
>>> url= ' http://www.baidu.com '
>>> response=requests.get ( URL)
>>> response.status_code
>>> response.headers[' content-type ']
' text/ html Charset=utf-8 '
>>> response.content
' <! DOCTYPE html><!--STATUS ok-->

Explanation: Requests uses the property name to get the corresponding property value
There are two subtle but important differences between 3.URLLIB2 and requests:
<1>requests automatically decodes the return information in Unicode
<2>requests automatically saves the return content, which can be read multiple times, while Urllib2.urlopen () returns only an object that is similar to a file type and can only be read once.

Three. Using requests (Import requests module: Import requests)
1. Send request:
Invokes the request method based on the requested type: Get, post, put, delete, head, options, such as:

>>> Import Requests
>>> r=requests.get (' http://www.baidu.com ')
>>> r= Requests.post (' Http://httpbin.org/post ')

2. pass parameters for URL
If the URL is built manually, the passed arguments are placed in the URL in the form of a "key/value" pair, such as Http://www.whut.com?name=ricky. Requests allows these parameters to be supplied in a dictionary using the "params" keyword parameter, for example:

>>> dict={' name ': ' Ricky ', ' Pass ': ' Secret '}
>>> r=requests.get (' http://www.whut.com ', params= Dict

3. Response content:
We can read the contents of the server response, such as:

>>> Import Requests
>>> r=requests.get (' Https://github.com/timeline.json ')
>>> R.text
u ' {"message": "Hello there, Wayfaring stranger." If You\u2019re Reading This then for you probably didn\u2019t your blog post a couple of years back announcing the this AP I would go away:http://git.i

Requests automatically decodes content from the server. When you visit "R.text", requests uses its inferred text encoding. Of course, you can find out what encoding requests uses and can use the "r.encoding" attribute to change it.

>>> r.encoding '
utf-8 '
>>> r.encoding= ' Unicode '

4. Binary response content :
For non-text requests, you can access the request response body in bytes:

>>> Import Requests
>>> r=requests.get (' http://c.hiphotos.baidu.com/image/w%3D400/sign= 7ae645d0d62a283443a6370b6bb7c92e/7dd98d1001e939013b80d8f379ec54e737d19678.jpg ')
>>> r.content
' \xff\xd8\xff\xe0\x00\x10jfif\x00\x01\x01\x01\x00\x01\x00\x01\x00\x00\xff\xdb\x00c\x00\x06\x04\x05\x06\x05\x04 \x06\x06\x05\x06\x07\x07\x06\x08\n\x10\n\n\t\t\n\x

5.JSON response content :
Requests also has a built-in JSON decoder that helps us with JSON data:

>>> Import Requests
>>> r=requests.get (' Https://github.com/timeline.json ')
>>> R.json ()
{u ' documentation_url ': U ' https://developer.github.com/v3/activity/events/#list-public-events ', U ' message ': U ' Hell

6. Custom Request Header
Sometimes it is necessary to add HTTP headers to the request (some sites may use the head to identify the type of program that sent the request), as long as simply passing a dictionary to the headers parameter is OK. How to get Headers content: You can use Chrome (right key-> review element->network-> Refresh page-> Select an element->headers)

>>> import re >>> import sys >>> reload (SYS) <module ' sys ' (built-in) > >>> SYS.S Etdefaultencoding (' gb18030 ') >>> type=sys.getfilesystemencoding () >>> headers={' user-agent ': ' mozilla/5.0 (IPhone; CPU iPhone os 8_0 like Mac os X applewebkit/600.1.3 (khtml, like Gecko) version/8.0 mobile/12a4345d '} > >> html.encoding= ' Utf-8 ' traceback (most recent call last): File "<stdin>", line 1, in <module> Nameer Ror:name ' HTML ' is not defined >>> html=requests.get (' http://www.wutnews.net/', headers=headers) >>> html.encoding= ' Utf-8 ' >>> print Html.text <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

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.