For convenience, use Chinaunix's account to get the account subject. Some sites may require a verification code to find some sites that do not have a verification code
The following ****** a lot of asterisks are private information, so use asterisks instead
#!/usr/bin/python#-*-encoding:utf-8-*-ImportUrllibImportUrllib2ImportCookielibImportReImportChardet #判断字符串编码的包 with a detailed example of Charset.detect (str) http://www.cnblogs.com/zhanhg/p/4392089.htmlfilename /c0>='Cookie.txt'#declares a Mozillacookiejar object instance to hold the cookie, and then writes the fileCookie =Cookielib. Mozillacookiejar (filename)#handler = Urllib2. Httpcookieprocessor (Cookie)#opener = Urllib2.build_opener (handler)Opener =Urllib2.build_opener (urllib2. Httpcookieprocessor (cookie)) PostData=Urllib.urlencode ({'Stuid':' *******', 'pwd':'*******' })#URL link for login ChinaunixLoginurl ='Http://bbs.chinaunix.net/member.php?mod=logging&action=login&logsubmit=yes'#impersonate a login and save the cookie to a variableresult =Opener.open (loginurl,postdata)#Save the cookie to the Cookie.txt fileCookie.save (Ignore_discard=true, ignore_expires=True)#links to queriesGradeurl ='http://bbs.chinaunix.net/home.php?m*********************'result=Opener.open (gradeurl) HTML=result.read () Reg= R'<a href\=\ "thread.*?" > (. *?) </a>'a=Re.findall (reg,html) forXinchA:Print(X.decode ('gb2312'))
Getting started with Python mini exercise 003 Get account information with Cookielib demo login