python urllib2无法在某些si上工作

2024-04-25 09:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

    import  urllib2

    def download(url,user_agent = 'wswp',num_retries=2):
        print 'downloading:',url
        headers = {'User-Agent': 'Mozilla/5.0'}
        request = urllib2.Request(url,headers=headers)
        try:
            html = urllib2.urlopen(request).read()
        except urllib2.URLError as e:
            print  "download error:"
            html = None
            if num_retries>0:
                if hasattr(e,'code') and 500<=e.code<600:
                    print "e.code = ",e.code
                    return download(url,num_retries-1)
        return  html
    print download("http://www.huaru.cc/mobile/product/xsim.html")

the result : C:\Python27\python.exe E:/py2_7/untitled1/secondClass_Agent downloading: http://www.huaru.cc/mobile/product/xsim.html

Process finished with exit code 0


Tags: httpurlreturnifrequestdownloadhtmlcode
1条回答
网友
1楼 · 发布于 2024-04-25 09:28:28

在Python中,缩进是关键。你知道吗

import urllib2


def download(url,user_agent = 'wswp',num_retries=2):
    print('downloading:', url)
    headers = {'User-Agent': 'Mozilla/5.0'}
    request = urllib2.Request(url, headers=headers)
    try:
        html = urllib2.urlopen(request).read()
    except urllib2.URLError as e:
        print("download error: {}".format(e))
        html = None
        if num_retries > 0:
            if hasattr(e, 'code') and 500 <= e.code < 600:
                print("e.code = ", e.code)
                return download(url, num_retries-1)
    return  html

print download("http://www.huaru.cc/mobile/product/xsim.html")

它显示如下:

('downloading:', 'http://www.huaru.cc/mobile/product/xsim.html')
download error: HTTP Error 404: Not Found
None

这是因为网页是返回404。你知道吗

它在python2.7.10和3.6上进行了测试

检查PEP8:https://www.python.org/dev/peps/pep-0008/#id17

相关问题 更多 >