Scrapy CrawlSpider 用于 AJAX 内容的爬取

13

我试图爬取一些新闻文章的网站。我的start_url包括:

(1) 每篇文章的链接:http://example.com/symbol/TSLA

(2) 一个“更多”按钮,它会发出AJAX调用,动态加载相同start_url中的更多文章:http://example.com/account/ajax_headlines_content?type=in_focus_articles&page=0&slugs=tsla&is_symbol_page=true

AJAX调用的一个参数是“page”,每次单击“More”按钮时都会递增。例如,单击“More”一次将加载额外的n篇文章,并在“More”按钮的onClick事件中更新页面参数,以便下次单击“More”时将加载第二页文章(假设最初加载了第0页,第一次单击加载了第1页)。

对于每个“page”,我想使用Rules来抓取每篇文章的内容,但我不知道有多少个“page”,也不想选择任意的m(例如10k)。我似乎无法弄清楚如何设置这个东西。

从这个问题Scrapy Crawl URLs in Order,我尝试创建一个潜在URL列表,但我无法确定如何以及在哪里从池中发送新的URL,在解析前一个URL并确保它包含CrawlSpider的新闻链接后。我的Rules将响应发送到parse_items回调函数,其中解析文章内容。

是否有一种方法可以在应用规则和调用parse_items之前观察链接页面的内容(类似于BaseSpider示例),以便我可以知道何时停止爬取?

简化代码(我删除了几个字段以使其更清晰):

class ExampleSite(CrawlSpider):

    name = "so"
    download_delay = 2

    more_pages = True
    current_page = 0

    allowed_domains = ['example.com']

    start_urls = ['http://example.com/account/ajax_headlines_content?type=in_focus_articles&page=0'+
                      '&slugs=tsla&is_symbol_page=true']

    ##could also use
    ##start_urls = ['http://example.com/symbol/tsla']

    ajax_urls = []                                                                                                                                                                                                                                                                                                                                                                                                                          
    for i in range(1,1000):
        ajax_urls.append('http://example.com/account/ajax_headlines_content?type=in_focus_articles&page='+str(i)+
                      '&slugs=tsla&is_symbol_page=true')

    rules = (
             Rule(SgmlLinkExtractor(allow=('/symbol/tsla', ))),
             Rule(SgmlLinkExtractor(allow=('/news-article.*tesla.*', '/article.*tesla.*', )), callback='parse_item')
            )

        ##need something like this??
        ##override parse?
        ## if response.body == 'no results':
            ## self.more_pages = False
            ## ##stop crawler??   
        ## else: 
            ## self.current_page = self.current_page + 1
            ## yield Request(self.ajax_urls[self.current_page], callback=self.parse_start_url)


    def parse_item(self, response):

        self.log("Scraping: %s" % response.url, level=log.INFO)

        hxs = Selector(response)

        item = NewsItem()

        item['url'] = response.url
        item['source'] = 'example'
        item['title'] = hxs.xpath('//title/text()')
        item['date'] = hxs.xpath('//div[@class="article_info_pos"]/span/text()')

        yield item
1个回答

13

如果您需要大量逻辑处理,使用爬虫可能会受到限制。在这种情况下,通常最好从Spider类继承。

Scrapy 提供了 CloseSpider 异常,当您需要在特定条件下停止解析时可以引发该异常。如果您正在爬取的页面返回消息“您的股票上没有重点文章”,当超过最大页面时,您可以检查此消息并在出现此消息时停止迭代。

在您的情况下,您可以使用以下内容:

from scrapy.spider import Spider
from scrapy.http import Request
from scrapy.exceptions import CloseSpider

class ExampleSite(Spider):
    name = "so"
    download_delay = 0.1

    more_pages = True
    next_page = 1

    start_urls = ['http://example.com/account/ajax_headlines_content?type=in_focus_articles&page=0'+
                      '&slugs=tsla&is_symbol_page=true']

    allowed_domains = ['example.com']

    def create_ajax_request(self, page_number):
        """
        Helper function to create ajax request for next page.
        """
        ajax_template = 'http://example.com/account/ajax_headlines_content?type=in_focus_articles&page={pagenum}&slugs=tsla&is_symbol_page=true'

        url = ajax_template.format(pagenum=page_number)
        return Request(url, callback=self.parse)

    def parse(self, response):
        """
        Parsing of each page.
        """
        if "There are no Focus articles on your stocks." in response.body:
            self.log("About to close spider", log.WARNING)
            raise CloseSpider(reason="no more pages to parse")


        # there is some content extract links to articles
        sel = Selector(response)
        links_xpath = "//div[@class='symbol_article']/a/@href"
        links = sel.xpath(links_xpath).extract()
        for link in links:
            url = urljoin(response.url, link)
            # follow link to article
            # commented out to see how pagination works
            #yield Request(url, callback=self.parse_item)

        # generate request for next page
        self.next_page += 1
        yield self.create_ajax_request(self.next_page)

    def parse_item(self, response):
        """
        Parsing of each article page.
        """
        self.log("Scraping: %s" % response.url, level=log.INFO)

        hxs = Selector(response)

        item = NewsItem()

        item['url'] = response.url
        item['source'] = 'example'
        item['title'] = hxs.xpath('//title/text()')
        item['date'] = hxs.xpath('//div[@class="article_info_pos"]/span/text()')

        yield item

2
谢谢!我是Scrapy的新手,觉得CrawlSpider是最好的方法。这个例子给了我建立基础的指导。 - BadgerBadgerBadger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接