在python 3中必须要导入的是 import urllib.request
python 2中则只要导入 import urllib
然后再写:
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
如果在python 3中使用第二条语句导入会有module 'urllib' has no attribute 'request'的模块错误
在python 2中只要导入第二条就可以了:
- import urllib
- def getHtml(url):
- page = urllib.urlopen(url)
- html = page.read()
- return html