【Python错误解决】No module named 'urllib2' 解决办法

版权声明:本文为博主原创文章,未经允许,不得转载!欢迎留言附带链接转载! https://blog.csdn.net/qq_15698613/article/details/88375508

主要问题出现在Python版本,你用的是3版本,写的代码还是2版本格式,举例子:

import urllib2  
response = urllib2.urlopen('http://www.123456.com/')  
html = response.read()  
print html  

报错: No module named 'urllib2' 

解决方法:

urllib.request代替urllib2

response.read()改为 resp.read()

print html 加括号
可以将代码换成:

import urllib.request
resp=urllib.request.urlopen('http://www.123456.com')
html=resp.read()
print(html)

猜你喜欢

转载自blog.csdn.net/qq_15698613/article/details/88375508