python 从 Web URL 读取文件 [英] python read file from a web URL

查看:91
本文介绍了python 从 Web URL 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试从网站读取 txt 文件.

I am currently trying to read a txt file from a website.

到目前为止我的脚本是:

My script so far is:

webFile = urllib.urlopen(currURL)

这样,我就可以处理文件了.但是,当我尝试存储文件(在 webFile 中)时,我只能获得到套接字的链接.我尝试的另一个解决方案是使用 read()

This way, I can work with the file. However, when I try to store the file (in webFile), I only get a link to the socket. Another solution I tried was to use read()

webFile = urllib.urlopen(currURL).read()

然而,这似乎删除了格式(\n\t 等)被删除.

However this seems to remove the formating (\n, \t etc) are removed.

如果我像这样打开文件:

If I open the file like this:

 webFile = urllib.urlopen(currURL)

我可以逐行阅读:

for line in webFile:
    print line

这将导致:

"this" 
"is" 
"a"
"textfile"

但我明白了:

't'
'h'
'i'
...

我希望在我的计算机上获取文件,但同时保持格式.

I wish to get the file on my computer, but maintain the format at the same time.

推荐答案

你应该使用 readlines() 来读取整行:

You should use readlines() to read entire line:

response = urllib.urlopen(currURL)
lines = response.readlines()
for line in lines:
    .
    .

但是,我强烈建议您使用 requests 库.此处链接 http://docs.python-requests.org/en/latest/

But, i strongly recommend you to use requests library. Link here http://docs.python-requests.org/en/latest/

这篇关于python 从 Web URL 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆