-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobo_crawler.py
49 lines (40 loc) · 1.68 KB
/
globo_crawler.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
47
48
49
import scrapy
import js2py
from scrapy.contrib.spiders import CrawlSpider
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class GloboCrawlerSpider(CrawlSpider):
name = 'globo-spider'
allowed_domains = ['g1.globo.com']
start_urls = ['https://g1.globo.com/']
custom_settings = {
'CLOSESPIDER_ITEMCOUNT': 100,
'DOWNLOAD_DELAY': 0.25,
'USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
}
rules = (
Rule(LinkExtractor(allow=r'/\d+/\d+/\d+/((\w+)-?)*'), callback='parse', follow=True),
)
def parse(self, response):
try:
script = response.xpath('//script[contains(., "RESOURCE")]/text()').extract_first()
context = js2py.EvalJs()
context.execute(script)
publish_date = context.cdaaas.SETTINGS.RESOURCE.ISSUED
except Exception as e:
print(e)
publish_date = None
for block in response.css('.content-head__title'):
if publish_date:
yield {'title': block.css('::text').extract_first(),
'url': response.url,
'publish_date': publish_date}
for next_page in response.css('.feed-post-body a'):
to = next_page.css('::attr(href)').extract_first()
if to:
yield response.follow(to, self.parse)
for next_page in response.css('.mc-article-body a'):
to = next_page.css('::attr(href)').extract_first()
if to:
yield response.follow(to, self.parse)