-
Notifications
You must be signed in to change notification settings - Fork 11
/
json_source.py
36 lines (25 loc) · 965 Bytes
/
json_source.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
from scraperx import Scraper, run_cli, Dispatch, Extract
class MyDispatch(Dispatch):
def submit_tasks(self):
return {'url': 'https://www.reddit.com/r/funny.json'}
class MyExtract(Extract):
def extract(self, raw_source, source_idx):
import json
yield self.extract_task(
name='products',
raw_source=json.loads(raw_source)['data']['children'],
callback=self.extract_posts,
post_extract=self.save_as,
post_extract_kwargs={'file_format': 'json'},
)
def extract_posts(self, item, idx, **kwargs):
return {'title': item['data'].get('title')}
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)