current position:Home>How do Python Network requests pass parameters

How do Python Network requests pass parameters

2022-01-30 04:35:21 Clever crane

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

There are two ways of network request GET and POST , In the browser developer tool , The Internet Network The module can easily capture all network requests initiated by the website , In the requested details , Request Method That is, the method of indicating the request is GET still POST.

We know , Network request mode GET and POST The most intuitive difference is , Different when passing parameters ,GET Include the parameter in URL in ,POST adopt request body Pass parameters .

This article mainly introduces how to use Python Of requests Library to initiate a network request ,GET and POST How the request passes parameters .

1. GET Parameters of

GET The requested parameters can be spliced directly in the url in , It can also be done through requests.get() Functional params Parameters to transmit .

Splicing URL The way , Form like

www.smartcrane.tech?kw=aaa&page=1&token=xxxxxxxx

domain name ? Parameters = value & Parameters = value

adopt params The way to pass the parameters ,

params = {
    "kw": "aaa",
    "page": 1,
    "token": "xxxxxx"
}

r = requests.get(url, params=params)
 Copy code 

2. POST Parameters of

POST The requested parameters can be through requests.post() Function data Parameters to transmit .

data = {
    "kw": "aaa",
    "page": 1,
    "token": "xxxxxx"
}

r = requests.post(url, data=data)
 Copy code 

copyright notice
author[Clever crane],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300435197038.html

Random recommended