簡體   English   中英

下一頁的抓取抓取不起作用

[英]Scrapy crawl with next page doesn't work

我想抓取一個網站,但是當我抓取下一頁時它不起作用,這是蜘蛛代碼? 哪里錯了,請告訴我,非常感謝。

import scrapy
from crawlAll.items import CrawlallItem

class ToutiaoEssayJokeSpider(scrapy.Spider):
    name = "duanzi"
    allowed_domains = ["http://duanziwang.com"]
    start_urls = ['http://duanziwang.com/category/duanzi/page/1']

    def parse(self, response):
        for sel in response.xpath("//article[@class='excerpt excerpt-nothumbnail']"):
            item = CrawlallItem()
            item['Title'] = sel.xpath("//header/h2/a/text()").extract_first()
            item['Text'] = sel.xpath("//p[@class='note']/text()").extract_first()
            item['Views'] = sel.xpath("//p[1]/span[@class='muted'][2]/text()").extract_first()
            item['Time'] = sel.xpath("//p[1]/span[@class='muted'][1]/text()").extract_first()
            yield item
        next_page = response.xpath("//ul/li[@class='next-page']/a/@href").extract_first()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)

我已經使用print(next_page)來測試next_page值是否為true,這是真的,它為我提供了這樣的鏈接地址: http : //duanziwang.com/category/duanzi/page/2 ',那又是什么我的代碼有誤嗎?

您的allowed_domains參數錯誤。 在這種情況下,它不應包含http,通常最好僅將域名保留在頂級域名(即domain.com)中

如果您運行蜘蛛並觀察日志,則會看到以下內容:

[scrapy] DEBUG: Filtered offsite request to 'duanziwang.com': <GET http://duanziwang.com/category/duanzi/page/2>

因此,請嘗試:

    allowed_domains = ["duanziwang.com"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM