提交正确信息时Urllib POST方法错误

时间:2013-11-12 07:39:45

标签: python

我是python的新手。我使用urllib将数据发布到网页,如果用户名&密码错误,Urllib不会引发错误并返回表示无效密码的html,但如果用户名为&密码正确,urllib引发错误。这是代码

WebScrapperPT.py

import cookielib
import urllib2
import urllib

class WebScrapperPT():
    UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11"
    CookieJar = None
    Cookies = None
    Opener = None
    ProxyUrl = ""
    ProxyUrlRaw = ""
    ProxyUrlPort = 80
    ProxyUsername = ""
    ProxyPassword = ""

    def Post(self, host, url, nvc, referer=""):
        self.SetOpener()
        encoded_nvc = urllib.urlencode(nvc)
        req = urllib2.Request(url, encoded_nvc)
        if (referer):
            req.add_header('Referer', referer)
        try:
            response = self.Opener.open(req)
            html = response.read()
        except Exception,e:
            html = str(e)
        return html

    def Initialize(self, proxyurl="", proxyurlraw="", proxyurlport=8080, username="", password=""):
        self.ProxyUrl = proxyurl
        self.ProxyUrlRaw = proxyurlraw
        self.ProxyUrlPort = proxyurlport
        self.ProxyUsername = username
        self.ProxyPassword = password
        self.CookieJar = cookielib.CookieJar()
        self.Cookies = urllib2.HTTPCookieProcessor(self.CookieJar)

    def SetOpener(self):
        self.CookieJar = cookielib.CookieJar()
        self.Cookies = urllib2.HTTPCookieProcessor(self.CookieJar)
        if (self.ProxyUrl):
            auth = "http://" + self.ProxyUsername + ":" + self.ProxyPassword +"@" + self.ProxyUrl
            handler = urllib2.ProxyHandler({'http' : auth})
            #self.Opener = urllib2.build_opener(handler, urllib2.HTTPRedirectHandler(), urllib2.HTTPHandler(debuglevel=0), urllib2.HTTPSHandler(debuglevel=0), urllib2.HTTPCookieProcessor(self.CookieJar))
            self.Opener = urllib2.build_opener(handler, self.Cookies)
        else:
            #self.Opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(), urllib2.HTTPHandler(debuglevel=0), urllib2.HTTPSHandler(debuglevel=0), urllib2.HTTPCookieProcessor(self.CookieJar))
            self.Opener = urllib2.build_opener(self.Cookies)

        if (self.UserAgent):
            self.Opener.addheaders = [('User-agent', self.UserAgent)]
        urllib2.install_opener(self.Opener)

    def GetHeader(self, host, referer):
        http_header = {
                "Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,text/png,*/*;q=0.5",
                "Accept-Language" : "en-us,en;q=0.5",
                "Accept-Charset" : "ISO-8859-1",
                "Content-type": "application/x-www-form-urlencoded",
                "Host" : host,
                "Referer" : referer
                }
        return http_header

Main.py

import StringOps
import WebScrapperPT

def main():
    wc = WebScrapperPT.WebScrapperPT()
    wc.Initialize("<company_proxy_url>", "<company_proxy_url>", 8080, "<username>", "<password>")

    str2 = wc.Post("www.tokyoinsider.net", "http://www.tokyoinsider.net/login",
                   {
                        "username" : "correctuser",
                        "password" : "correctpassword",
                        "remember" : "on",
                        "login" : "Log+In",
                        "n" : ""
                   }, "http://www.tokyoinsider.net/login/")
    print str2

if __name__ == '__main__':
    main()

响应= self.Opener.open(req)将抛出异常,说“如果正确的用户和correctpassword是有效的登录,则无法将类型'Microsoft.Scripting.Runtime.Uninitialized'的对象强制转换为”system.exception“”到那个网站,如果类型是无效登录,它不会抛出错误。

0 个答案:

没有答案