使用Python从cloudflare下载网站文件

时间:2016-04-29 22:42:09

标签: python web-scraping cloudflare downloading

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

2 个答案:

答案 0 :(得分:3)

我明白了。

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)

答案 1 :(得分:1)

这是从'csv'文件下载多个文件,其中包含链接。

注意:我从这里得到了帮助:Python download files by links stored in csv     import cfscrape     import csv,sys     导入请求     import urllib2     import os

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 exist"
    except csv.Error as e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
相关问题