- Use existing cookies to access the website
Import cookielib, urllib2
Ckjar = cookielib. cookillacookiejar (OS. Path. Join ('C: \ Documents and Settings \ Tom \ Application Data \ Mozilla \ Firefox \ profiles \ h5m61j1i. default', 'cookies.txt '))
Req = urllib2.request (URL, postdata, header)
Req. add_header ('user-agent ',\
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )')
Opener = urllib2.build _ opener (urllib2.httpcookieprocessor (ckjar ))
F = opener. Open (req)
Htm = f. Read ()
F. Close ()
- Access the website to obtain the cookie and save it in the cookie file.
Import cookielib, urllib2
Req = urllib2.request (URL, postdata, header)
Req. add_header ('user-agent ',\
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )')
Ckjar = cookielib. mozillacookiejar (filename)
Ckproc = urllib2.httpcookieprocessor (ckjar)
Opener = urllib2.build _ opener (ckproc)
F = opener. Open (req)
Htm = f. Read ()
F. Close ()
Ckjar. Save (ignore_discard = true, ignore_expires = true)
- Generate a cookie using the specified parameter and use the cookie to access the website
Import urllib. request as urimport urllib. parse as up # import HTTP. cookiejar as CJ # Coding: gbkcharset = 'utf8' # component information # cookiejar = CJ. cookiejar () # urlopener = Ur. build_opener (Ur. httpcookieprocessor (cookiejar) urlopener = Ur. build_opener (Ur. httpcookieprocessor () # jiayuan configuration information name = 'yatere 'uid = '000000' # HTTP header headers = {'user-agent': 'mozilla/22017518 (compatible; MSIE 4.0; windows NT 5.1; Trident/4.0 ;. net CLR 2.0.507 27 ;. net CLR 3.0.04506.30) '} seachhead = {'user-agent': 'mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. net CLR 2.0.50727 ;. net CLR 3.0.04506.30) ', 'Referer': 'http: // search.jiayuan.com/', 'origin': 'http: // search.jiayuan.com '} # User Login datavalues = {'Password': '000000', 'name': 'yin _ kai@163.com'} # search datasearchdata = {'sex ': 'F', 'work _ location': '42', 'work _ sublocation ': '000000', 'min _ age': '22 ',' Max_age ': '26', 'min _ height': '000000', 'max _ height': '000000', 'ucation': '20 ', 'Edu _ more_than ': 'on', 'astro': '0', 'animal': '', 'bloodtype': '0', 'Welcome ': '0', 'house': '0', 'auto': '0', 'marriage': '1', 'Children ': '0', 'level ': '0', 'industry ': '0', 'Company': '0', 'home _ location': '42', 'home _ sublocation ': '123 ', 'nation': '0', 'belief': '0', 'ques _ love': '0', 'Avatar ': 'on', 'Save _ name ': ''} # Post Data Conversion Program def data (Val UES): Data = up. urlencode (values ). encode () return data # access the specified page def geturlcon (URL, Data = none, headers = headers): Request = Ur. request (URL, Data, headers) url = urlopener. open (request) page = URL. read (). decode ('utf8', 'ignore') return page # Check whether logon is successful def checklogin (page): If page. find (name)> 0: Return true Elif page. find (UID)> 0: Return true else: Return false # access the logon page (obtain cookie) url1 = 'HTTP: // login.jiayuan.com/dologin. PHP 'geturlcon (url1, data (values), headers) # access other pages url2 = 'HTTP: // www.jiayuan.com/usercp/'page?geturlcon (url2) If checklogin (page ): print ('login successfully') else: Print ('login failed') url3 = 'HTTP: // profile.jiayuan.com/14214171'page?geturlcon (url3) If checklogin (page): Print (url3, 'login successfully') else: Print ('login failed') # view search results url3 = 'HTTP: // search.jiayuan.com/result.php? T = 10 & M = 1' page = geturlcon (url3, data (searchdata), seachhead) If checklogin (page): Print (url3, 'login successful ') else: print ('logon failed ')