文章内容

2017/7/2 9:06:01,作 者: 黄兵

Python3 error: “Import error: No module name urllib2”

Here's my code:

import urllib2.request

response = urllib2.urlopen("https://pdf-lib.org")
html = response.read()
print(html)

Any help?

解决方法:

For a script working with Python 2 (tested versions 2.7.3 and 2.6.8) and Python 3 (3.2.3 and 3.3.2+) try:

#! /usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib import urlopen

html = urlopen("http://www.google.com/")
print(html.read())
分享到:

发表评论

评论列表