您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Python读取URL的内容?

如何使用Python读取URL的内容?

要回答您的问题:

import urllib

link = "http://www.somesite.com/details.pl?urn=2344"
f = urllib.urlopen(link)
myfile = f.read()
print(myfile)

您需要read(),而不是readline()

编辑(2018-06-25):自Python 3起,旧版urllib.urlopen()被替换为urllib.request.urlopen()(有关详细信息,请参阅https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen中的注释) 。

或者,只需在此处获取此库:http ://docs.python-requests.org/en/latest/并认真使用它即可:)

import requests

link = "http://www.somesite.com/details.pl?urn=2344"
f = requests.get(link)
print(f.text)
python 2022/1/1 18:33:07 有197人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶