ModuleNotFoundError: No module named 'urllib2'

版权声明:本文为博主原创文章,未经博主允许也可以得转载。 https://blog.csdn.net/zrcshendustudy/article/details/82316921

历尽千辛万苦,终于在Eclipse上安装好了python的编译工具了

https://blog.csdn.net/zrcshendustudy/article/details/82120397

正准备来一个爬虫程序入门的时候,报了这么一个错。

Traceback (most recent call last):
  File "D:\Users\ASUS\workspace\PaChong\hello\hello.py", line 1, in <module>
    import urllib2
ModuleNotFoundError: No module named 'urllib2'

我写的代码

import urllib2

if __name__ == '__main__':
 print(urllib.urlopen("http://www.baidu.com").read())

由于我装的python是3.7版本的,而urllib2是python2.0以上的版本。

感谢这篇博客的指导,让我知道urllib2这个库已经在Python3.7版本不适用了。

https://blog.csdn.net/sinat_36246371/article/details/55188891

所以正确的程序为:

import urllib.request

print(urllib.request.urlopen("http://www.baidu.com").read())

这样子就是最简单的python爬虫程序了。

另一种写法:

import urllib.request

response=urllib.request.urlopen("http://www.baidu.com");
print(response.read());

猜你喜欢

转载自blog.csdn.net/zrcshendustudy/article/details/82316921
今日推荐