在把舊的 python 2.x code 搬到 python 3.x 時,遇到 error:
Import error: No module name urllib2
程式碼:
import urllib2.request
response = urllib2.urlopen("http://www.google.com")
html = response.read()
print(html)
解法:
https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2
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 urllib2 import urlopen
html = urlopen("http://www.google.com/")
print(html.read())