Several Requests library requests: Requests library requests

Source: Internet
Author: User

Several Requests library requests: Requests library requests

The content of this article Source: https://www.dataquest.io/mission/117/working-with-apis

Data source: https://en.wikipedia.org/wiki/International_Space_Station

Abstract: The github repository is operated through the requests library and github api to familiarize yourself with the process of interacting with network data.

Application Program Interface (API) is used to query and return dynamic data on the network, such as stock price information and real-time news comments. The process of retrieving data through APIS is like when you access a Web page, submitting a request to the server, and then the server returns the corresponding data. Usually use the requests Library (http://www.python-requests.org/en/latest/) to submit the request, the github API address used in this article is: https://developer.github.com/v3/

Get access token

To operate your github through APIS, you must first generate an access token on the github URL (https://github.com/settings/tokens)

# Retrieving authorized user information import requestsheaders = {"Authorization": "975ef3a9dff1a7dc50d5f0a4341ecf5a89f201ab"} # ignore user = requests in subsequent code. get ('https: // api.github.com/user', headers = headers ). json () print user # If the get () method does not contain the headers parameter, the following result is returned: (authentication required) {u'documentation _ url': u'https: // pai.github.com/v3', u'message ': u'requires authentication '} but not all methods must carry the headers parameter to obtain personal information (replace kylinlin with any account name) response = requests. get ("https://api.github.com/users/kylinlin") print response. status_code print response. json ()

Each access generates a status code, which is represented by the status_code attribute. This status code is used to indicate the access result:

The content returned by a request is in string format by default. to extract some information in the content, the most convenient method is to convert the string into a python object, and the json () method is used to implement this function.

Add the parameter requests in the get method. the params parameter in the get () method is the parameter in the url. Different from the headers parameter, the value of the headers parameter is written in the request header, and the value of the params parameter is written in the url, for example, you need to get the project that the github author is interested in, and take 50 results as one page to get the content on the first page. At this point the url is actually like this: https://api.github.com/users/kylinlin/starred? Pre_page = 50 & page = 1

params = {"per_page": 50, "page": 1}response = requests.get("https://api.github.com/users/kylinlin/starred", params=params)page1_repos = response.json()print page1_repos

 

POST request

The post request is used to send information to the server, and then create an object on the server, for example, to create a new project

Payload = {'name': 'learning-about-apis '} # the value of the name parameter is the name of the project to be created. status = requests. post ('https: // api.github.com/user/repos', headers = headers, json = payload ). status_code # The json parameter converts the payload variable to the dictionary format print status # output 201 indicates that the project is successfully created.

Payload = {'description': 'learning about requests! ', 'Name': 'learning-about-apis'} status = requests. patch ('https: // api.github.com/repos/kylinlin/learning-about-apis', json = payload, headers = headers ). status_codeprint status #200 is returned after the patch method is successful

Status = requests. delete ('https: // api.github.com/repos/kylinlin/learning-about-apis', headers = headers). status_code print status # The deletion is successful if 204 is output.

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.