Python crawler learning-timeout setting

In the case that the local network is not good or the network response is too slow, in order to prevent waiting too long, a timeout period can be set, that is, if there is no response after this time, an error will be reported. Need to use the timeout parameter. The calculation of this time refers to the time from when a request is issued to the server returning a response .

Persimmon chestnuts are as follows:

import requests
r = requests.get("https://www.taobao.com",timeout=1)
print(r.status_code)

Through the above method, we can set the timeout time to 1 second, and if there is no response within 1 second, an exception will be thrown.

In fact, the request is divided into two phases, namely connect and read

The timeout set above will be used as the sum of the timeout for connection and reading.

If you want to specify separately , you can pass in a tuple:

r=requests.get('https://www.taobao.com',timeout=(5,11))

If you want to wait forever , you can set the timeout to None, or leave it blank without setting, because the default is None.

Guess you like

Origin blog.csdn.net/m0_46437725/article/details/114904374