Requests get data from the API and put it in MySQL

Source: Internet
Author: User

Python's requests library is a very powerful library, and the requests installation method is simple, using:

PIP Install requests

To install requests, after successful installation:

Import Requests

You can import the requests module, requests have get and post two ways:

1. Requests.get () Usage:

 "http://xxx"== a_content.content   #. The content and. Text function is the same
= Json.loads (AA)

Here is the json.loads and Json.dumps, which is the two functions of the JSON module, respectively decoding and encoding JSON data, Json.loads converts a JSON data object to a Python object, instead, Json.dumps converts a Python object to a JSON data object

The A_json we get is a dictionary nested list structure, with the dictionary key can get its corresponding value, and then can parse each field, such as:

Version_name = a_json[0]['latest_release'['package'] [  'name']
A_json is a list, the No. 0 element in the list is a dictionary, which takes the key in the dictionary, and finally gets the version information.

2. Requests.post () Usage:

Requests.post () usage is basically the same as Requests.get (), but Requests.post can accept requests with parameters and can contain information such as token:

Payload = {            "Access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxx",            "Request_body": {}}headers= {'Content-type':'Application/json'}url= R"http://xxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxx"Re= Requests.post (URL, data=json.dumps (payload), headers=headers) R= Json.loads (Re.text)

Python connection mysqldb:

DB1 =MySQLdb.connect (Host="localhost", DB="", the user="Root", passwd="", Port=3306, CharSet='UTF8') Cur2=db1.cursor () Cur2.execute ('xxxxxxxxxxx') SQL1="""CREATE TABLE Table_deploy (ID INT (one) UNSIGNED not null PRIMARY KEY auto_increment, AppID INT NOT NULL                                  , timestamp VARCHAR (+), deploy_id INT )"""Cur2.execute (SQL1) db1.commit ()

This is the original method for Python to connect to MySQL, and if you use Django, the Orm,django models internally encapsulates the MySQL method

MySQLdb.connect () indicates that the database connection is open, anddb1.cursor () means that the cursor () method is used to get the operation cursors,cur2.execute (' xxxxxxxxxxx ') represents executes the SQL statement using the Execute method,SQL1 =xxx represents the creation of the data table SQL statement

Timestamp-related conversions, we know that the time taken from the interface is often not the time format we want, which requires time conversion:

now_time = Int (Time.time ())  # timestamp of the current time
" %y-%m-%d%h:%m ")  

# Timestamp is the time string,strptime The function resolves the time string to a time tuple according to the specified format
timestamp_1 = Int (time.mktime (timearray))
#将时间元组转换为时间戳
timeArray11 = time.localtime (timestamp_1)
#利用localtime () converts a timestamp to a time array
Datestr = Time.strftime ("%y-%m-%d%h:%m:%s", timeArray1)
#再将时间数组转换为指定格式的时间字符串
Datetimeobj = Datetime.datetime.strptime (Datestr, "%y-%m-%d%h:%m:%s")
#将字符串日期转换为datetime
Data_hours_after = Datetimeobj + Datetime.timedelta (hours=16)
# date can be added and reduced after conversion to DateTime, indicating a date after 16 hours

Detailed sample code See my Github:https://github.com/a342058040/requests-mysql-.git

Requests get data from the API and put it in MySQL

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.