scrapy - 我究竟做错了什么 ?我想让我的蜘蛛使用 URL 爬到下一页
问题描述
我是scrapy的新手。我正在研究简单的教程。一切正常,除了我无法爬到下一页。
import scrapy
class QuoteSpider(scrapy.Spider):
name = "quotes"
start_urls=['http://quotes.toscrape.com']
allowed_domains = ["quotes.toscrape.com"]
def parse(self,response):
for response in response.xpath('//div[@class="quote"]'):
yield {
"quote":response.xpath('./span[@class="text"]/text()').extract(),
"author" : response.xpath('./span/small[@class="author"]/text()').extract(),
"tag" : response.xpath('./div[@class="tags"]/a/text()').extract()
}
next_page = response.xpath('//nav/ul[@class="pager"]/li[@class="next"]/a/@href').extract_first()
if next_page is not None:
next_page_url = response.urljoin(next_page)
yield scrapy.Request(url=next_page_url,callback=self.parse)
我的错误信息:
next_page_url = response.urljoin(next_page)
AttributeError:“选择器”对象没有属性“urljoin”
解决方案
问题是您正在使用 for 循环覆盖响应对象。因此,for 循环中的内部响应对象只是spidy.language.path_node.PathNode
不包含 urljoin 定义的类型。这应该可以解决您的问题。
for response_path in response.xpath('//div[@class="quote"]'):
yield {
"quote":response_path.xpath('./span[@class="text"]/text()').extract(),
"author" : response_path.xpath('./span/small[@class="author"]/text()').extract(),
"tag" : response_path.xpath('./div[@class="tags"]/a/text()').extract()
}
next_page = response_path.xpath('//nav/ul[@class="pager"]/li[@class="next"]/a/@href').extract_first()
if next_page is not None:
next_page_url = response.urljoin(next_page)
yield scrapy.Request(url=next_page_url,callback=self.parse)
推荐阅读
- loops - Vue:避免重复计算
- firebase - 无法确定任务“:app:preDebugBuild”的依赖关系
- ios - 在 UI 测试中找到 UIRefreshControl
- angular - 错误:多个自定义值访问器与未指定名称属性的表单控件匹配
- javascript - 使用 Javascript 使用类名填充元素
- hadoop-yarn - yarn logs 抛出“not valid bcfile”错误
- ios - ARKIT 如何获得世界原点变换
- dependencies - 如何使用所有依赖项静态安装 vlc 包?
- c# - 如何根据具有 linq 表达式的第一个表中的值从不同的表中获取记录?
- javascript - 带有依赖/链式自动完成列表的 Bootstrap 中的 Select2。不工作