Learn the installation and use of requests from reptiles

Requests is a third-party library for Python. Its installation is very simple, just run the following code in the pathcm terminal

pip install requests

You can also run the following code in cmd to install the Requests library

python -m pip install requests

There are many request methods in web pages, such as get, post, etc., you can check to find the request method, and obtain the page code according to the request method

For parameter description:

url: the URL you want to grab

resp: the fetched result

import requests
url = 'http://baidu.com'
resp = requests.get(url)
print(resp.text)
resp.close()

 There are several parameters in get

res = requests.get(url,headers=headers,params,timeout)

The parameters are described as follows:

  • url: The url address to grab.
  • headers: used to wrap request header information.
  • params: The query string parameters carried in the request.
  • timeout: Timeout time, an exception will be thrown if the timeout is exceeded.

And post request:

resp=requests.post(url,data={请求体的字典})

Get and post are relatively common request methods. Learn to use it to access most web pages

Guess you like

Origin blog.csdn.net/qq_62392385/article/details/126950968