-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathqa_results.py
More file actions
43 lines (33 loc) · 1.35 KB
/
qa_results.py
File metadata and controls
43 lines (33 loc) · 1.35 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
37
38
39
40
41
42
43
from scraperx import Scraper, run_cli, Dispatch, Extract
class MyDispatch(Dispatch):
def submit_tasks(self):
return {'url': 'https://www.imdb.com/chart/top/'}
class MyExtract(Extract):
def extract(self, raw_source, source_idx):
yield self.extract_task(
name='products',
selectors=['tbody.lister-list tr'],
callback=self.extract_product,
post_extract=self.save_as,
post_extract_kwargs={'file_format': 'json'},
qa={
'title': {'required': True, 'max_length': 128},
'year': {'type': int, 'required': True},
'rating': {'type': float},
}
)
def extract_product(self, element, idx, **kwargs):
return {
'title': element.css('td.titleColumn a').xpath('string()').extract_first(),
'year': int(element.css('span.secondaryInfo').xpath('string()').extract_first()[1:-1]),
'rating': float(element.css('td.ratingColumn ').xpath('string()').extract_first()),
}
my_scraper = Scraper(dispatch_cls=MyDispatch,
extract_cls=MyExtract)
if __name__ == '__main__':
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(name)s - [%(scraper_name)s] %(message)s'
)
run_cli(my_scraper)