Climber —- foundation

Request module:

More documents: http://cn.python-requests.org/zh_CN/latest/ p>

Install

 pip install requests

Use

import requests

response=requests.get("https://movie.douban.com/cinema/nowplaying/beijing/")

Parameters
print(response.content) # byte data print(response.text) # character data print(type(response)) # print(response.status_code) # 200 print(response.encoding) # utf-8 print(response.cookies) # ]>

 

GET request:

If you want to request a JSON file, you can use the json() method to parse

response=requests.get("https://github.com/timeline.json")

print(response.text)
print(response.json().get("message"))

Original response content

If you want to get the original socket response from the server, You can get r.raw. But you need to set stream=True in the initial request.

>>> r = requests.get('https://github.com/timeline.json', stream=True)
>>> r.raw

>>> r.raw.read(10)
‘\X1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03’

In general, you should save the text stream to a file in the following mode:

with open(filename,'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk) 

Customized request header

headers={ "Content-Type":"application/json"}
data={"username":"yuan"}
response=requests.get("https://movie.douban.com/cinema/nowplaying/beijing/",params=data,headers=headers)

print(response.url)
print(response.headers)

 

POST request:

payload = {'key1': 'value1','key2':'value2'}
response = requests.post("http://httpbin.org/post", data=payload)
print(response.text)

You can also pass in a meta for the data parameter Group list. This method is especially effective when multiple elements in the form use the same key:

import requests
payload = ((‘key1’, ‘value1’), (‘key1’, ‘value2’))
# payload = {"k":"v"}
r = requests.post(‘http://httpbin.org/post’, data=payload)
print(r.text)

Use json Parameters are passed directly

Method 1:

import requests
import json
url = ‘https://api.github.com/some/endpoint’
payload = {‘some‘: ‘data’}
r = requests.post(url, data=json.dumps(payload))

Method two

import requests
url = ‘https://api.github.com/some/endpoint’
payload = {‘some‘: ‘data’}
r = requests.post(url, json=payload)

requests upload files

import requests
url = ‘http://httpbin.org/post’
files = {‘file‘: open(‘test‘, ‘rb‘)}
r = requests.post(url, files=files)
print(r.text)

pip install requests

import requests

response=requests.get("https://movie.douban.com/cinema/nowplaying/beijing/")

Parameters
print(response.content) # byte data print(response.text) # character data print(type(response)) # print(response.status_code) # 200 print(response.encoding) # utf-8 print(response.cookies) # ]>

response=requests.get("https://github.com/timeline.json")

print(response.text)
print(response.json().get("message"))

>>> r = requests.get('https://github.com/timeline.json', stream =True)
>>> r.raw

>>> r.raw.read(10)
‘\X1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03’

with open(filename, ‘wb‘) as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk) 

headers={"Content-Type":"application/json"}
data={"username":"yuan"}
response=requests.get("https://movie.douban.com/cinema/nowplaying/beijing/",params=data,headers=headers)

print(response.url)
print(response.headers)

payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
response = requests.post("http://httpbin.org/post", data=payload)
print(response.text)

import requests
payload = ((‘key1’, ‘value1’), (‘key1’, ‘value2’))
# payload = {"k":"v"}
r = requests.post(‘http://httpbin.org/post’, data=payload)
print(r.text)

import requests
import json
url = ‘https://api.github.com/some/endpoint’
payload = {‘some‘: ‘data’}
r = requests.post(url, data=json.dumps(payload))

import requests
url = ‘https://api.github.com/some/endpoint’
payload = {‘some‘: ‘data’}
r = requests.post(url, json=payload)

import requests
url = ‘http://httpbin.org/post’
files = {‘file‘: open(‘test‘, ‘rb‘)}
r = requests.post(url, files=files)
print(r.text)

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2035 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.