通过BeautifulSoup来登陆人人网。可以通过info = {‘email‘:‘‘,‘password‘:‘‘}进行账号密码的初始化,一个BeautifulSoup的简单应用,过一阶段会写一个关于BeautifulSoup框架使用的小手册,欢迎大家关注啊,求各路读者大大多加指导。
#-*- coding:utf-8 -*- import urllib import urllib2 import cookielib import re from BeautifulSoup import BeautifulSoup #获取人人网账号的名称 def getTitle(page): pattern = re.compile('<title>(.*?)</title>',re.S) matchResult = re.search(pattern,page) if matchResult and matchResult.group(1): return matchResult.group(1) url = "http://www.renren.com/SysHome.do" response1 = urllib2.urlopen(url) source = response1.read() soup1 = BeautifulSoup(source) log_url = soup1('form',{'method':'post'})[0]['action'] #info = {'email':'XXXX','password':'XXXX'} #账号密码必须在运行之前指定 info = {'email':'','password':''} #LWPCookieJar提供可读写操作的cookie文件,存储cookie对象 cookiejar = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) urllib2.install_opener(opener) try: response2 = urllib2.urlopen(log_url,urllib.urlencode(info)) text = response2.read() print "+"*20+"welcome to 京东放养的爬虫"+"+"*20 print "%s成功登陆人人网。。。"%getTitle(text) print "+"*20+"welcome to 京东放养的爬虫"+"+"*20 except urllib2.URLError,e: if hasattr(e,'reason'): print "reason:[0]".format(e.reason) if hasattr(e,'code'): print "code:[0]".format(e.code)
python模拟登陆人人网(通过BeautifulSoup module)
原文地址:http://blog.csdn.net/djd1234567/article/details/45295307