python 시뮬레이션 인인인망 접속(Beautiful Soup module 통해)
1631 단어 python 네트워크 파충류
#-*- coding:utf-8 -*-
import urllib
import urllib2
import cookielib
import re
from BeautifulSoup import BeautifulSoup
#
def getTitle(page):
pattern = re.compile('(.*?) ',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)