python - 卡在从页面中提取标题和下一页 url
问题描述
我正在尝试提取这些搜索结果中每个 rv 单元详细信息页面的链接以及搜索结果的下一页,以便我可以获得他们在该站点上的每个 rv 单元的链接
import scrapy
class cwscrape(scrapy.Spider):
name = 'rvlinks'
start_urls = ['https://rv.campingworld.com/searchresults?condition=new_used&custompricerange=true&custompaymentrange=true&sort=featured_asc&zipsearch=true&search_mode=advanced&locations=nationwide']
def parse(self, response):
for rvname in response.xpath("//div[@class='title']"):
yield{ 'rv_full_name': rvname.xpath(".//span[@itemprop='name']/text()").extract_first()}
next_page= response.xpath(".//div[@class='pagination-wrap']/a/@href").extract_first()
if next_page is not None:
next_page_link= response.urljoin(next_page)
yield scrapy.Request(url=next_page_link, callback=self.parse)
每个详细信息单元的示例网址为:
https ://rv.campingworld.com/rvdetails/new-class-c-rvs/2019-thor-freedom-elite-26he-front-living-60k-BKY1571461
解决方案
我已经尝试过您的代码scrapy shell
,一切看起来都很好:
In [5]: response.xpath("//div[@class='title']//span[@itemprop='name']/text()").extract()
Out[5]:
[u'2019 THOR FREEDOM ELITE 22HEC',
u'2018 THOR GEMINI 23TR',
u'2018 THOR GEMINI 23TK',
u'2019 THOR FREEDOM ELITE 24HE',
u'2019 WINNEBAGO MINNIE WINNIE 22R',
u'2019 WINNEBAGO MINNIE WINNIE 22M',
u'2019 WINNEBAGO OUTLOOK 27D',
u'2019 THOR FREEDOM ELITE 28FE',
u'2019 WINNEBAGO MINNIE WINNIE 25B',
u'2019 THOR FREEDOM ELITE 28FE',
u'2019 WINNEBAGO OUTLOOK 31N',
u'2019 THOR QUANTUM RC25',
u'2018 THOR SYNERGY JR24',
u'2019 WINNEBAGO MINNIE WINNIE 26A',
u'2019 THOR QUANTUM KM24',
u'2019 WINNEBAGO MINNIE WINNIE 31G',
u'2019 THOR SYNERGY 24SJ',
u'2019 WINNEBAGO VIEW 24G',
u'2019 WINNEBAGO VIEW 24V',
u'2019 WINNEBAGO OUTLOOK 22E']
In [6]: response.xpath(".//div[@class='pagination-wrap']/a/@href").get()
Out[6]: u'https://rv.campingworld.com/searchresults?condition=new_used&custompricerange=true&custompaymentrange=true&sort=featured_asc&zipsearch=true&search_mode=advanced&locations=nationwide&scpc=&make=&landingMake=0&page=1'
你遇到了什么样的问题?
推荐阅读
- python - 使用python以最佳方式查找列表的最小总和
- solr - solr 中全文搜索和自由文本搜索的区别(其他搜索数据库)
- python-3.x - Python 3 中的简单导入
- java - 这个错误是什么意思?模拟器:FramebufferData::restore: 警告:纹理被删除而不解除绑定 FBO
- laravel - Laravel / Forge 服务器如何确定哪个 Github 用户获取公钥?
- rust - 如何使用 Tokio 实现基于拉的系统?
- python - ValueError:行或列值必须至少为 1
- python - Itertools - 合并两个列表以获得所有可能的组合
- python - 我可以在一个程序中多次调用函数'main'吗?
- architecture - 是否有架构路线图、流程图或图表之类的东西,用于根据项目要求何时使用某些技术?