pythonシミュレーションログインサイトでデータをキャプチャ

1462 ワード

#coding=utf-8
import re
import urllib
import urllib2
import cookielib
import urllib,urllib2,cookielib
import re
class xiaobai:
    post_data=""#       
    def __init__(self):
        '''    ,   cookies '''
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        opener.addheaders = [('User-agent', 'Opera/9.23')]
        urllib2.install_opener(opener)

    def login(self,loginurl,bianma):
        '''    '''
        req = urllib2.Request(loginurl,self.post_data)
        _response = urllib2.urlopen(req)
        _d=_response.read()
        _d =_d.decode(bianma)
        return _d

    def getpagehtml(self,pageurl,bianma):
        '''             html  '''
        req2=urllib2.Request(pageurl)
        _response2=urllib2.urlopen(req2)
        _d2=_response2.read()
        _d2 =_d2.decode(bianma)
        return _d2
if __name__=="__main__":
        x=xiaobai()
        #    post  
        x.post_data=urllib.urlencode({'uname':'  ','pass':'123','op':'login'})
        y=x.login("http://www.lvye.org/user.php","utf-8")#  
        #       html   
        print x.getpagehtml("http://www.lvye.org/userinfo.php?uid=111111","utf-8")

転載先http://blog.csdn.net/yybjroam05/article/details/6851920