python文本文件下载文件夹

2024-05-28 18:42:12 发布

您现在位置:Python中文网/ 问答频道 /正文

目标是从https://transitfeeds.com/p/agence-metropolitaine-de-transport/129/latest/download开始,通过python web抓取下载GTFS数据

目前,我正在使用requests,如下所示:

def download(url):
    fpath = "prov/city/GTFS"
  
    r = requests.get(url)

    if r.ok:
        print("Saving file.")
        open(fpath, "wb").write(r.content)

    else:
        print("Download failed.")

不幸的是,上述url的requests.content结果呈现以下内容:

Screenshot of requests response.

您可以在输出中看到感兴趣的文件(例如stops.txt),但我如何访问它们进行读/写


Tags: httpscomurl目标downloaddecontentrequests
3条回答

以下方面发挥了作用:

def download(url):
    fpath = "path/to/output/"


    f = requests.get(url, stream = True, headers = headers)

    if f.ok:
        print("Saving to {}".format(fpath))
        g=open(fpath+'output.zip','wb')
        g.write(f.content)
        g.close()

    else:
        print("Download failed with error code: ", f.status_code)

您需要将此文件写入zip

import requests

url = "https://transitfeeds.com/p/agence-metropolitaine-de-transport/129/latest/download"
fname = "gtfs.zip"

r = requests.get(url)
open(fname, "wb").write(r.content)

现在fname已经存在,并且里面有几个文本文件。如果希望以编程方式提取此zip文件,然后读取文件的内容,例如stops.txt,则首先需要extract单个文件,或者只需extractall

import zipfile

# this will extract only a single file, and
# raise a KeyError if the file is missing from the archive
zipfile.ZipFile(fname).extract("stops.txt")

# this will extract all the files found from the archive,
# overwriting files in the process
zipfile.ZipFile(fname).extractall()

现在,您只需要处理您的文件

thefile = "stops.txt"

# just plain text
text = open(thefile).read()

# csv file
import csv

reader = csv.reader(open(thefile))
for row in reader:
   ...

我担心您正在尝试使用文本编辑器读取zip文件,也许您应该尝试使用"zipfile" module

相关问题 更多 >

    热门问题