-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoproblems.py
More file actions
37 lines (27 loc) · 1.06 KB
/
Copy pathfoproblems.py
File metadata and controls
37 lines (27 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import scrapy
class FOSpiderProblems(scrapy.Spider):
name = "foproblems"
start_urls = [
"http://fyzikalniolympiada.cz/archiv/zadani-a-reseni",
]
def parse(self, response):
links = response.css('a[href$=".pdf"]::attr(href)').getall()
problems_and_solutions = [link for link in links if 'archiv' in link] # Chceme jen archiv, ne současné dokumenty
for link in problems_and_solutions:
file_name = os.path.basename(link)
yield scrapy.Request( # Stahujeme PDF
url=response.urljoin(link),
callback=self.save_pdf,
meta={'title': file_name}
)
def save_pdf(self, response):
"""Stahuje PDF soubor"""
title = response.meta['title']
filename = f"{title}.pdf"
download_dir = "Ulohy/"
if not os.path.exists(download_dir):
os.makedirs(download_dir)
with open(download_dir + filename, 'wb') as f:
f.write(response.body)
self.log(f"Saved file {filename}")