繁体   English   中英

如何从网站提取数据并将其保存到txt文件中

[英]how to extract the data from from website and save the into txt file

我正在使用python 3.3.3并尝试提取数据以及txt文件中的链接。 我已经尝试过此代码,但它既未创建也未在文件中保存任何内容。在控制台上,如果我要写“ print(s)”,则仍然无法显示任何内容。

import urllib.request
with urllib.request.urlopen("http://www.python.org") as url:
    s = url.read()
    url.close()
#I'm guessing this would output the html source code?
#print(s)

# write data
ff = open("ne.txt", "w")
ff.write('s')
ff.close()

如果我做错了,请告诉我正确的标准,因为我也在理解和学习互联网资源。

任何帮助请...

您可以使用urllib.request.urlretrieve(url, path_to_file)

所有代码:

import urllib.request
urllib.request.urlretrieve("https://www.python.org", "/home/user/ne.txt")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM