鱼C论坛

 找回密码
 立即注册
查看: 2812|回复: 18

用scrapy爬豆瓣top250的问题

[复制链接]
回帖奖励 3 鱼币 回复本帖可获得 1 鱼币奖励! 每人限 1 次(中奖概率 50%)
发表于 2020-7-22 14:40:36 | 显示全部楼层

回帖奖励 +1 鱼币

  1. import scrapy
  2. from douban.items import DoubanItem

  3. class DoubanSpiderSpider(scrapy.Spider):
  4.     name = 'douban'
  5.     allowed_domains = ['douban.com']
  6.     start_urls = ['https://movie.douban.com/top250']

  7.     def parse(self, response):
  8.         content = response.xpath('//ol/li')
  9.         
  10.         for each in content:
  11.             item = DoubanItem()
  12.             item['rank'] = each.xpath('//em/text()').extract()
  13.             item['name'] = each.xpath('//div[@class="hd"]/a/span[1]/text()').extract()
  14.             item['info'] = each.xpath('//p/text()').extract()
  15.             item['scort'] = each.xpath('//div[@class="star"]/span[2]/text()').extract()

  16.             yield item

  17.         if len(response.xpath('//span[@class="next"]/a/@href')) != 0 :
  18.             next_url = response.xpath('//span[@class="next"]/a/@href').extract()[0]

  19.             yield scrapy.Request('https://movie.douban.com/top250' + next_url,callback = self.parse,dont_filter = False)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-1 02:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表