鱼C论坛

 找回密码
 立即注册
查看: 2575|回复: 2

[已解决]scrapy翻页问题:实现不了翻页,第一页 爬完就停止了,检查不出有啥问题,求助大佬

[复制链接]
发表于 2019-1-21 21:53:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import scrapy
  2. import urllib.parse
  3. import requests

  4. class TbSpider(scrapy.Spider):
  5.     name = 'tb'
  6.     allowed_domains = ['baidu.com']
  7.     start_urls = [
  8.         'https://tieba.baidu.com/mo/q---3237548E300DB3D76551F88E33D2ABB1%3AFG%3D1--1-3-0--2--wapp_1548077552162_707/m?kw=%E9%92%A6%E5%B7%9E%E5%AD%A6%E9%99%A2&lp=5011&lm=&pn=0'
  9.     ]

  10.     def parse(self, response):
  11.         i_list = response.xpath('//div[contains(@class,"i")]')
  12.         for i in i_list:
  13.             item = {}
  14.             item['title'] = i.xpath('./a/text()').extract_first()
  15.             item['href'] = i.xpath('./a/@href').extract_first()
  16.             item['href'] = urllib.parse.urljoin(response.url, item['href'])
  17.             item['img_list'] = []
  18.             # item['content_list'] = []
  19.             yield scrapy.Request(
  20.                 url=item['href'],
  21.                 callback=self.parse_detail,
  22.                 meta = {'item':item}
  23.             )
  24.         # 列表页下一页
  25.         next_url = response.xpath('//a[text()="下一页"]/@href').extract_first()
  26.         if next_url is not None:
  27.             next_url = urllib.parse.urljoin(response.url, next_url)
  28.             scrapy.Request(
  29.                 url=next_url,
  30.                 callback=self.parse
  31.             )

  32.     def parse_detail(self, response):
  33.         item = response.meta['item']
  34.         # item['content_list'].extend(response.xpath('//div[@class="i"]//text()').extract())
  35.         # item['img_list'] = [urllib.parse.urljoin(response.url,i) for i in item['img_list']]
  36.         # item['img_list'] = [requests.utils.unquote(i).split('src=')[-1] for i in item['img_list']]
  37.         # item['img_list'].extend(response.xpath('//div[@class="i"]/a/@href').extract())
  38.         
  39.         # 详情页下一页
  40.         next_url = response.xpath('//a[text()="下一页"]/@href').extract_first()
  41.         if next_url is not None:
  42.             next_url = urllib.parse.urljoin(response.url, next_url)
  43.             yield scrapy.Request(
  44.                 url=next_url,
  45.                 callback=self.parse_detail,
  46.                 meta={'item':item}
  47.             )
  48.         else:
  49.             print(item)
  50.             # yield item
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-22 02:19:18 | 显示全部楼层    本楼为最佳答案   
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-1-22 12:15:18 | 显示全部楼层

谢谢这位大哥,我真眼拙
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 10:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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