1. Can
r.headers
get response header content by
>>>R = requests. Get(' http://www.zhidaow.com ')>>> R. Headers{ ' content-encoding ': ' gzip ', ' transfer-encoding ': ' chunked ', ' content-type ': ' text/html; Charset=utf-8 '; ... }< /c8>
>>> R. Headers[' Content-type ']
' text/html; Charset=utf-8 '
>>> r. Headers. Get(' Content-type ')
< Span class= "o" > ' text/html; Charset=utf-8 '
2. Set the time-out period
>>> requests. Get(' http://github.com ', timeout=0.001)
3. Delegate Access
/span>
{ "http""http://10.10.1.10:3128" "https" "http://10.10.1.10:1080",}requests . Get("http://www.zhidaow.com"proxies=proxies)
This is required if the agent requires an account and password :
{ "http""Http://user:[email protected]:3128/",}
4. Settings that do not allow redirection
>>>requests. Get(' Http://www.baidu.com/link?url=QeTRFOS7TuUQRppa0wlTJJr6FfIYI1DJprJukx4Qy0XnsDO_s9baoO8u1wvjxgqN ' False)
5. Uploading Files
>>>Url=' Http://httpbin.org/post '>>>files = { ' file ' : open ( ' Report.xls ' ' RB ' )}>>> r = requestspost (urlfiles =files) >>> r. Text{ "files": { "file": "<censored...binary...data>" },
/span>
you can also explicitly set the file name:
>>>Url=' Http://httpbin.org/post '>>>Files={ ' file ' : ( ' Report.xls ' open ( ' Report.xls ' ' RB ' >>> r = requests< Span class= "O". post (urlfiles =files) >>> r. Text{ "files": { "file": "<censored...binary...data>" }, /span>
If you want, you can also send a string as a file to receive :
>>>Url=' Http://httpbin.org/post '>>>Files={' File ': ( ' report.csv ' "some,data,to, Send\nanother,row,to,send\n ' )}>>> r = requests.< span class= "n" >post (urlfiles=< span class= "n" >files) >>> r. Text{ "files": { "file": "some,data,to,send\\nanother,row,to,send\\n" }, /span>
Python web crawler