Scrapy CrawlSpider用于AJAX内容

时间:2014-05-16 23:46:04

标签: python web-scraping scrapy

我正在尝试抓取网站以获取新闻报道。我的start_url包含:

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

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

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

对于每个“页面”,我想使用规则来抓取每篇文章的内容,但我不知道有多少“页面”,我不想选择一些任意的m(例如,10k)。我似乎无法弄清楚如何设置它。

从这个问题Scrapy Crawl URLs in Order开始,我尝试创建潜在网址的网址列表,但在解析之前的网址并确保其确定后,我无法确定从池中发送新网址的方式和位置包含CrawlSpider的新闻链接。我的规则发送对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 个答案:

答案 0 :(得分:11)

抓取蜘蛛可能对您的目的而言太有限了。如果你需要很多逻辑,你通常最好不要继承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