相关疑难解决方法(0)

urllib2试试,除了404

我正在尝试使用urlib2浏览一系列编号的数据页面.我想要做的是使用一个try语句,但我对它知之甚少,通过阅读一点判断,它似乎是基于特定的"名称",这是例外,例如IOError等.我不知道是什么我正在寻找错误代码,这是问题的一部分.

我写了/粘贴了'urllib2缺少的手册'我的urllib2页面获取程序因此:

def fetch_page(url,useragent)
    urlopen = urllib2.urlopen
    Request = urllib2.Request
    cj = cookielib.LWPCookieJar()

    txheaders =  {'User-agent' : useragent}

    if os.path.isfile(COOKIEFILE):
        cj.load(COOKIEFILE)
        print "previous cookie loaded..."
    else:
        print "no ospath to cookfile"

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    try:
        req = urllib2.Request(url, useragent)
        # create a request object

        handle = urlopen(req)
        # and open it to return a handle on the url

    except IOError, e:
        print 'Failed to open "%s".' % url
        if hasattr(e, 'code'):
            print 'We failed with error code - %s.' …
Run Code Online (Sandbox Code Playgroud)

python exception urllib2 python-2.x

12
推荐指数
2
解决办法
4万
查看次数

request.get()404响应后,requests_HTTPError未被捕获

我的请求库有点问题.

比方说,我在Python中有这样的声明:

try:
   request = requests.get('google.com/admin') #Should return 404

except requests.HTTPError, e:
   print 'HTTP ERROR %s occured' % e.code
Run Code Online (Sandbox Code Playgroud)

由于某种原因,异常没有被捕获.我已经检查了API文档中的请求,但它有点渺茫.是否有人对图书馆有更多经验可以帮助我?

python error-handling try-catch python-requests

2
推荐指数
1
解决办法
1万
查看次数