Skip to content

Third-party Python Libraries

All third-party Python libraries can be found on PYPI / Python Package Index and installed using PIP:

Bash
1
2
3
4
5
# Install the latest version of requests
pip install requests

# Install a specific version of requests
pip install requests==2.28.1

This article will introduce several commonly used third-party libraries in Python.

1. HTTP Requests (requests library)

The requests library is a library used to send HTTP requests, with the official website at requests.readthedocs.io.

1.1 Sending GET Requests and Getting the Response Body

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import requests

# Send an HTTP Get request
resp = requests.get('https://httpbin.org/get')

# Check the response code, throw an error if it's 4xx or 5xx
resp.raise_for_status()

# Response code
print('Response result resp.status_code is:', resp.status_code)

# Directly output the response body text
data = resp.text
print('Type of resp.text:', type(data))
print('Content of resp.text:', data)

# Parse the response body as JSON
data = resp.json()
print('Type of resp.json():', type(data))
print('Content of resp.json():', data)

Output:

Text Output
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Response result resp.status_code is: 200
Type of resp.text: <class 'str'>
Content of resp.text: {
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118d02-7473a7f52d01e431446e6d2a"
  },
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/get"
}

Type of resp.json(): <class 'dict'>
Content of resp.json(): {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.28.1', 'X-Amzn-Trace-Id': 'Root=1-63118d02-7473a7f52d01e431446e6d2a'}, 'origin': '101.132.183.69', 'url': 'https://httpbin.org/get'}

1.2 Sending More Complex Requests

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests

# Specify Query parameters when sending
params = { 'user-id': 'u-001' }
resp = requests.get('https://httpbin.org/get', params=params)
print('Specify Query parameters when sending:', resp.text)

# Specify headers when sending
headers = { 'X-User-Id': 'u-001' }
resp = requests.get('https://httpbin.org/get', headers=headers)
print('Specify headers when sending:', resp.text)

# Specify timeout when sending
resp = requests.get('https://httpbin.org/get', timeout=3)
print('Specify timeout when sending:', resp.text)

# Ignore HTTPS certificate verification (use when requesting self-signed domains)
resp = requests.get('https://httpbin.org/get', verify=False)
print('Ignore HTTPS certificate verification (use when requesting self-signed domains):', resp.text)

# Send POST request with Form format body
data = { 'key': 'value' }
resp = requests.post('https://httpbin.org/post', data=data)
print('Send POST request with Form format body:', resp.text)

# Send POST request with JSON format body
data = { 'key': 'value' }
resp = requests.post('https://httpbin.org/post', json=data)
print('Send POST request with JSON format body:', resp.text)

Output:

Text Output
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Specify Query parameters when sending: {
  "args": {
    "user-id": "u-001"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118f09-083cd24569089d0b24dc78b8"
  },
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/get?user-id=u-001"
}

Specify headers when sending: {
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118f0a-5cbb1a1a0f1dc8b46d648575",
    "X-User-Id": "u-001"
  },
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/get"
}

Specify timeout when sending: {
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118f0a-6c6cf1b94cc4c1144faa7054"
  },
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/get"
}

/usr/local/lib/python3.8/dist-packages/urllib3/connectionpool.py:842: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn((
Ignore HTTPS certificate verification (use when requesting self-signed domains): {
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118f0b-632853b91c5290d55f49bef4"
  },
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/get"
}

Send POST request with Form format body: {
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "key": "value"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "9",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118f0c-4e725597001f988434096767"
  },
  "json": null,
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/post"
}

Send POST request with JSON format body: {
  "args": {},
  "data": "{\"key\": \"value\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "16",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.28.1",
    "X-Amzn-Trace-Id": "Root=1-63118f0d-66486dcd4c6889b61d4249c9"
  },
  "json": {
    "key": "value"
  },
  "origin": "101.132.183.69",
  "url": "https://httpbin.org/post"
}

2. Time Handling (arrow library)

The arrow library is a library for handling time, with the official website at arrow.readthedocs.io.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import arrow

t = arrow.utcnow().timestamp
print('Get current Unix timestamp:', t)

t = arrow.now('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
print('Get current Beijing time string:', t)

t = arrow.now('Asia/Shanghai').isoformat()
print('Get current Beijing time ISO8601 formatted string:', t)

t = arrow.get(1577808000).to('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
print('Parse from Unix timestamp and output Beijing time string:', t)

t = arrow.get('2019-12-31T16:00:00Z').to('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
print('Parse from ISO8601 time string and output Beijing time string:', t)

t = arrow.get('2020-01-01 00:00:00', 'YYYY-MM-DD HH:mm:ss').replace(tzinfo='Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
print('Parse from non-standard time string and use as Beijing time string:', t)

t = arrow.get('2019-12-31T16:00:00Z').shift(days=-1).to('Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss')
print('Time calculation: get previous day and output Beijing time string:', t)

Output:

Text Output
1
2
3
4
5
6
7
Get current Unix timestamp: 1662095305
Get current Beijing time string: 2022-09-02 13:08:25
Get current Beijing time ISO8601 formatted string: 2022-09-02T13:08:25.920213+08:00
Parse from Unix timestamp and output Beijing time string: 2020-01-01 00:00:00
Parse from ISO8601 time string and output Beijing time string: 2020-01-01 00:00:00
Parse from non-standard time string and use as Beijing time string: 2020-01-01 00:00:00
Time calculation: get previous day and output Beijing time string: 2019-12-31 00:00:00

3. XML to JSON (xmltodict library)

The xmltodict library is a library used to convert XML data into JSON, with the Github address at github.com/martinblech/xmltodict.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import xmltodict
import json

data = '''
<root>
    <name>张三</name>
    <age>25</age>
    <job>程序员</job>
    <job>项目经理</job>
</root>
'''
data = xmltodict.parse(data)
print('Result after parsing XML to JSON:', data)
print('Result after parsing XML to JSON and outputting as JSON string:', json.dumps(data, indent=2, ensure_ascii=False))

Output:

Text Output
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Result after parsing XML to JSON: {'root': {'name': '张三', 'age': '25', 'job': ['程序员', '项目经理']}}
Result after parsing XML to JSON and outputting as JSON string: {
  "root": {
    "name": "张三",
    "age": "25",
    "job": [
      "程序员",
      "项目经理"
    ]
  }
}