我的爬虫学习之路------urllib(urllib2整合到了urllib)

urllib2在python3后已经合并在urllib中了,具体为urllib.response,urllib.request
urllib2.URLError 改为了urllib.error.URLError

URLError与HTTPError

关于这一点我看了另一个博主的,直接上他的URL吧,点击传送阿谋

下载网页(requests库也可以)

from urllib import request
from urllib import error

'''
urllib2在python3后已经合并在urllib中了,具体为urllib.response,urllib.request
'''


def download(url):
    print('Downloading:', url)
    try:
        html = request.urlopen(url).read()
    except error.URLError as e:
        print('Download error', e.reason)
        html = None
    return html


if __name__ == '__main__':
    url = 'http://202.115.47.141/'
    print(download(url))
发布了82 篇原创文章 · 获赞 235 · 访问量 108万+

猜你喜欢

转载自blog.csdn.net/solitudi/article/details/104718420
今日推荐