![](/img/trans.png)
[英]How to download (PDF) files with Python/Scrapy using the Files Pipeline?
[英]Scrapy won't download files with the standard Files Pipeline
我正在尝试从College Moodle下载所有文件,这些文件是按课程组织的。 我设法以正确的顺序成功登录并找到了文件的所有链接; 当我将链接传递给parse_files
函数时,没有下载任何内容,并且得到以下输出:
2018-07-11 21:56:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://moodle.some.ac.il/course/view.php?id=1032> (referer: https://moodle.some.ac.il/my/)
2018-07-11 21:56:41 [scrapy.core.scraper] DEBUG: Scraped from <200 https://moodle.some.ac.il/course/view.php?id=1032>
{'files_urls': ['https://moodle.some.ac.il/course/view.php?id=1032#section-0']}2018-07-11 21:56:44 [scrapy.core.engine] INFO: Closing spider (finished)
2018-07-11 21:56:44 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 5944,
'downloader/request_count': 18,
'downloader/request_method_count/GET': 17,
'downloader/request_method_count/POST': 1,
'downloader/response_bytes': 220875,
'downloader/response_count': 18,
'downloader/response_status_count/200': 16,
'downloader/response_status_count/303': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 7, 11, 18, 56, 44, 591156),
'item_scraped_count': 13,
'log_count/DEBUG': 32,
'log_count/INFO': 7,
'memusage/max': 53354496,
'memusage/startup': 53354496,
'request_depth_max': 2,
'response_received_count': 16,
'scheduler/dequeued': 17,
'scheduler/dequeued/memory': 17,
'scheduler/enqueued': 17,
'scheduler/enqueued/memory': 17,
'start_time': datetime.datetime(2018, 7, 11, 18, 56, 37, 753571)}
2018-07-11 21:56:44 [scrapy.core.engine] INFO: Spider closed (finished)
我在启用内置文件管道并在我的设置中设置FILES_STORE = 'files'
后使用了内置文件管道。 我可能缺少某些东西,或者与以下事实有关:每个文件链接https://moodle.some.ac.il/course/view.php?id=1032#section-0
指向https://moodle.some.ac.il/pluginfile.php/44033/mod_resource/content/1/my_file.docx
,蜘蛛程序无法解决重定向链接吗? 也许有什么更好的方法? 非常感谢。 这是我的蜘蛛:
import scrapy
import os
import college_files.items as my_item
class CollegeSpider(scrapy.Spider):
name = 'college_spider'
start_urls = ['https://moodle.somecollege.com']
if not os.path.exists('files'):
os.makedirs("files")
def parse(self, response):
return scrapy.FormRequest.from_response(
response,
formdata={'username': 'my_username', 'password': 'my_password'},
callback=self.after_login
)
def after_login(self, response):
user_name = response.xpath('//div/header/div/div/h1/text()').extract()
cursos_first = response.css('div.well')
if not os.path.exists(myFilesDir):
os.makedirs(myFilesDir)
for my_course in cursos_first.css('a'):
my_course_dir = os.path.join(myFilesDir, my_course.css('a::text').extract_first())
if not os.path.exists(my_course_dir):
os.makedirs(my_course_dir)
yield response.follow(my_course.css('a::attr(href)').extract_first(), callback=self.parse_files)
print("Despues1 {0}".format(my_course))
def parse_files(self, response):
topics = response.css('ul.topics')
sections = topics.css('li.section')
my_i = my_item.FileToDown()
my_i['files_urls'] = [sections.css('a::attr(href)').extract_first()]
yield my_i
没有很多信息真的很难说,没有我自己的moogle帐户我将无法获得更多信息,但是如果问题是 scrapy不能自动解决重定向问题,您可能想添加一个额外的步骤您的抓取管道,在请求https://moodle.some.ac.il/course/view.php?id=1032#section-0之后,它会在其中将请求构造到文件的真实位置。
总而言之,这个想法是编写一个函数,该函数接受请求链接的响应,例如https://moodle.some.ac.il/course/view.php?id=1032#section-0,并使用此常量响应以构建对诸如https://moodle.some.ac.il/pluginfile.php/44033/mod_resource/content/1/my_file.docx的链接的请求,然后由您的parse_files
函数处理其响应。
为了弄清楚该中间函数必须执行什么操作才能提交对所需文件url的请求,我建议检查将传递给中间函数的响应的正文,并将xpath定位到文件url所在的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.