我尝试编写代码,该代码可以打开url连接并从响应中读取文本。我试过了:
import urllib
urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345')
但这给了我这个错误:
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345')
AttributeError: 'module' object has no attribute 'urlopen'
我的代码出什么问题了,该问题的解决方案是什么?
我认为问题在于您使用的是Python 3.x,但使用的代码仅适用于2.x。
在Python 3.x中,该urlopen
函数包含在urllib.request
:
>>> from urllib.request import urlopen
>>> urlopen
<function urlopen at 0x020DA7C8>
>>>
编辑:
我认为这可以满足您的所有需求:
from urllib.request import urlopen
page = urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345').read()
print(page)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句