Python | Use of the requests module

requests モジュールの使い方

ブラウザを使ってサーバーにリクエストを送信するだけでなく、サードパーティモジュール requests を使って、コードでサーバーにリクエストを送信し、結果を取得することもできます。

# The requests module is a third-party module that can be used to send network connections
# pip install requests

import requests

response = requests.get('http://127.0.0.1:8090')
# print(response) The result is a Response object

# content refers to the returned result, which is a binary and can be used to transfer pictures
# print(response.content.decode('utf8')) # decode binary into string

# The obtained result is a text
print(response.text)

print(response.status_code) # 200

# If the returned result is a json string, the json string can be parsed
# print(response.json())

r = requests.get('http://127.0.0.1:8090/test')
t = r.text # Get the json string
print(t, type(t)) # {"name": "zhangsan", "age": 18}

j = r.json() # parse the json string into the corresponding data type in python
print(j, type(j)) # {'name': 'zhangsan', 'age': 18}

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone お問い合わせ
Hi, I'm Alibaba Cloud AI Assistant!
I can help with questions and solutions.