在python 2.7中使用urllib3从url下载.txt文件?

在python 2.7中使用urllib3从url下载.txt文件?,python,beautifulsoup,python-requests,shutil,urllib3,Python,Beautifulsoup,Python Requests,Shutil,Urllib3,我使用的是Python 2.7,我有urllib3。我正在尝试下载此链接中的每个.txt文件: 这是我的密码: #!/usr/bin/env python # -*- coding: utf-8 -*- from bs4 import BeautifulSoup import requests import urllib3, shutil http = urllib3.PoolManager() MTA_url = requests.get("http://web.mta.info/de

我使用的是Python 2.7,我有urllib3。我正在尝试下载此链接中的每个.txt文件:

这是我的密码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
import requests
import urllib3, shutil


http = urllib3.PoolManager()

MTA_url = requests.get("http://web.mta.info/developers/turnstile.html").text
MTA_soup = BeautifulSoup(MTA_url)
#Find each link to be downloaded
MTA_soup.findAll('a')
#Let's test it with the 36th link
one_a_tag = MTA_soup.findAll("a")[36]
MTA_link = one_a_tag["href"]

download_url = 'http://web.mta.info/developers/'+ MTA_link
print download_url #valid url, will take you to download
这就是我被困的地方。我似乎不知道如何在
download\u url
下载.txt文件,更不用说遍历列表了。我试过这个:

open('/Users/me/Documents/test_output_download.csv', 'wb').write(download_url.content)
但这给了我一个错误:

AttributeError: 'unicode' object has no attribute 'content'
在进一步阅读之后,我还尝试:

out_file = '/Users/me/Documents/test_output_download.csv'
http.request('GET', download_url, preload_content=False) as res, open(out_file, 'wb') as out_file:
   shutil.copyfileobj(res, out_file)
但我克服了这个语法错误:

    http.request('GET', download_url, preload_content=False) as res, open(out_file, 'wb') as out_file:
                                                              ^
SyntaxError: invalid syntax

如何使用urllib3下载位于
download\u url
的.txt文件并将其保存到本地驱动器?提前感谢。

导入时使用“as”关键字。我测试了完整的代码段,在这里做了一个小改动后就可以下载了

尝试将其转换为将对象声明为变量,如下所示:

res = http.request('GET', download_url, preload_content=False)

out_file = open(out_file, 'wb')
shutil.copyfileobj(res, out_file)

您在阅读的源代码中可能没有注意到有一个
with
。使用Python 2有什么特殊原因吗?它用于导入,但也用于异常处理和
with
语句。问题中的代码看起来像是一个带有语句的破损的
。ThomThio-谢谢!这起作用了。我不明白为什么需要整个shutil.copyfileobj—为什么不做一些更简单的事情,比如这项工作呢打开('/Users/me/Documents/test_output_download.csv',wb')。编写(download_url.content),您需要将该行分解为其组件。下载url是一个字符串-在通过res=http.request('GET',download\u url,preload\u content=False)调用http.request之前,此字符串后面没有任何结果(例如内容)。此后,“res”保存着一个csv,可以说,它不是一个可以逐行将其写入所需文件的字符串。因此,您需要shutil.copyfileobj按顺序读取并将其写入已知的文件格式。为清楚起见,@user2357112supportsMonica指的是另一种称为打开文件的方式,如果您在声明前添加with也可以。谢谢,您的解释很有意义。