使用 Python 使用 cloudflare 从网站下载文件

download files from website with cloudflare using Python

我正在尝试使用 python 从具有 Cloud Flare 的网站下载 mp3 文件。我知道 python 的 'cfscrape' 模块,但如何使用它从 url.

下载文件

我知道了。

import cfscrape

scraper = cfscrape.create_scraper()

url = 'the website url'
cfurl = scraper.get(url).content
name = url.split('/')[-1]

with open(name, 'wb') as f:
    f.write(cfurl)

此处用于从具有链接的 'csv' 文件下载多个文件。

注意:我从这里得到了帮助:

import csv
import os
import sys

import cfscrape

scraper = cfscrape.create_scraper()

filename = 'nazm_urls.csv'
with open(filename, 'rb') as f:
    reader = csv.reader(f)
    try:
        for row in reader:
            if 'http' in row[0]:
                reverse = row[0][::-1]
                i = reverse.index('/')
                tmp = reverse[0:i]
                cfurl = scraper.get(row[0]).content
                if not os.path.exists("./"+tmp[::-1]):
                    with open(tmp[::-1], 'wb') as f:
                        f.write(cfurl)
                        f.close()
                else:
                    print("file: ", tmp[::-1], "already exists")
    except csv.Error as e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))