Python3.7中urllib.urlopen 报错问题

import urllib
web = urllib.urlopen('https://www.baidu.com')
f = web.read()
print(f)

报错:

Traceback (most recent call last):
  File "D:/coding/python/text.py", line 2, in
    web = urllib.urlopen('https://www.baidu.com')
AttributeError: module 'urllib' has no attribute 'urlopen'

Process finished with exit code 1


解决方案:Python3里的urllib模块已经发生改变,此处的urllib都应该改成urllib.request。

 

更改后的代码

import urllib.request
web = urllib.request.urlopen('https://www.baidu.com')
f = web.read()
print(f)

结果显示:

Python3.7中urllib.urlopen 报错问题_第1张图片

 

 运行成功

转载于:https://www.cnblogs.com/yswyzh/p/9555722.html

你可能感兴趣的:(Python3.7中urllib.urlopen 报错问题)