python自动抓取_Python Scrapy 自动抓取下一页内容

最近在学下Scrapy,抓取下一页的时候遇到了问题

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)

具体代码如上,我只能抓取第一页的12条内容,第二页的连接我用print的时候也能打印出来,说明连接是获取成功了,就是:

next_page = response.urljoin(next_page)

yield scrapy.Request(next_page, callback=self.parse)

这两句代码没有回过头去调用parse,不知道为什么?请大神帮忙看看,谢谢了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值