/
spider.py
46 lines (36 loc) · 1.69 KB
/
spider.py
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
37
38
39
40
41
42
43
44
45
46
import json
import scrapy
import page_parser
import storage
import wallpapers_parser
class Spider(scrapy.Spider):
name = 'wallpapers page spider'
start_urls = [
'https://www.smashingmagazine.com/category/wallpapers/',
'https://www.smashingmagazine.com/category/wallpapers/page/2/',
'https://www.smashingmagazine.com/category/wallpapers/page/3/',
'https://www.smashingmagazine.com/category/wallpapers/page/4/',
'https://www.smashingmagazine.com/category/wallpapers/page/5/',
'https://www.smashingmagazine.com/category/wallpapers/page/6/',
'https://www.smashingmagazine.com/category/wallpapers/page/7/',
'https://www.smashingmagazine.com/category/wallpapers/page/8/',
'https://www.smashingmagazine.com/category/wallpapers/page/9/',
'https://www.smashingmagazine.com/category/wallpapers/page/10/',
'https://www.smashingmagazine.com/category/wallpapers/page/11/',
]
def parse(self, response):
wallpapers_collection_page_paths = response.xpath('//article/h1/a/@href').extract()
for page_path in wallpapers_collection_page_paths:
url = response.urljoin(page_path)
if 'wallpaper' in url:
yield scrapy.Request(url, self.handle_page)
def handle_page(self, response):
html = response.body.decode("utf-8")
wallpapers_nodes = page_parser.parse_page(html)
date = page_parser.extract_date(html)
wallpapers = wallpapers_parser.parse_wallpapers(wallpapers_nodes)
for wallpaper in wallpapers:
wallpaper.year = date['year']
wallpaper.month = date['month']
storage.create()
storage.save(wallpapers)