Because of some relationship, I need to discard the lovely requests library and then use URLLIB2, have to say, the function, or requests powerful Ah, cookie,session,put,option and so on,,,,
Well, back to the point, due to functional needs, I need to obtain a request after the cookie, not how to use URLLIB2, just this study, I use cookielib and urllib2 cooperation. Use Cookielib to create a file that stores cookies, and then read the contents of the cookie after initiating the request. The specific code is as follows:
import urllib2import cookielibheaders = { ' user-agent ': ' mozilla/5.0 (windows nt 6.1; win64; x64; rv:53.0) gecko/20100101 firefox/53.0 ', ' Referer ': ' http://www.baidu.com ', }cj = cookielib. Cookiejar () opener = urllib2.build_opener (urllib2. Httpcookieprocessor (CJ) Urllib2.install_opener (opener) Urllib2.socket.setdefaulttimeout (Ten) target = ' http://127.0.0.1/cms/phpcms/' Response = urllib2. Request (target, '/', headers) #利用request发起带data和headers的请求 #data not required in this time, so use '/' instead of Opener.open (response) for i in cj: print i.name print i.value #循环读取, remove the cookie
In fact, I want to write the script at the same time to launch three times to take cookies with a request for cookies, my ability is limited, really can not solve, give up, but read the cookie is still possible, hmm. And then we'll try harder.
This article is from the "hesitated" blog, make sure to keep this source http://chichu.blog.51cto.com/11287515/1973862
"Learning Notes" urllib2 and use of cookies