Python Scrapy找不到div.title

Python Scrapy找不到div.title,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,所以我用这个蜘蛛来练习拉网,我试图收集这一页上所有书籍的标题。当我进入终点站并进入 刮壳'http://books.toscrape.com/catalogue/page-1.html" 然后 response.css(“div.title”).getall() 它只返回一个空列表 [] 如有任何澄清,我们将不胜感激。正如在评论中指出的那样,不存在类别为标题的div 页面上每本书的完整标题位于a标记(锚定标记)的title属性中,锚定标记链接到该特定书籍的页面 您可以为所有具有title属性的锚

所以我用这个蜘蛛来练习拉网,我试图收集这一页上所有书籍的标题。当我进入终点站并进入

刮壳'http://books.toscrape.com/catalogue/page-1.html"

然后

response.css(“div.title”).getall()

它只返回一个空列表

[]

如有任何澄清,我们将不胜感激。

正如在评论中指出的那样,不存在
类别为
标题的div

页面上每本书的完整标题位于
a
标记(锚定标记)的
title
属性中,锚定标记链接到该特定书籍的页面

您可以为所有具有
title
属性的锚定标记获取
title
属性的值,如下所示:

import scrapy


class BookSpider(scrapy.Spider):
    name = "books"
    start_urls = [
        'http://books.toscrape.com/catalogue/page-1.html'
    ]

    def parse(self, response):
        page = response.url.split(".")[-1]
        filename = f'BooksHTML-{page}.html'
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log(f'Saved file {filename}') 
返回:

response.css("a::attr(title)").getall()

答案是正确的。该页面不包含任何具有类“title”的标记。你看过消息来源了吗?我怀疑您想要包含“title”属性的标记。这是一个不同的查询,不涉及css。那么我如何检索标题呢?因此正确的语法是response.ccs(“HtmlTag.ClassName SubTag::attr(AttributeName)”).getall()?
css
而不是
ccs
,但除此之外,是的,这是正确的。您在问题中尝试的语法也是正确的,只是没有任何元素与选择器匹配。太棒了,非常感谢您只需使用[title]@QHarr您是对的。查找标题不需要这篇文章,谢谢您指出这一点。我已经调整了我的答案。
['A Light in the Attic', 'Tipping the Velvet', 'Soumission', 'Sharp Objects', 'Sapiens: A Brief History of Humankind', 'The Requiem Red', 'The Dirty Little Secrets of Getting Your Dream Job', 'The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull', 'The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics', 'The Black Maria', 'Starving Hearts (Triangular Trade Trilogy, #1)', "Shakespeare's Sonnets", 'Set Me Free', "Scott Pilgrim's Precious Little Life (Scott Pilgrim #1)", 'Rip it Up and Start Again', 'Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991', 'Olio', 'Mesaerion: The Best Science Fiction Stories 1800-1849', 'Libertarianism for Beginners', "It's Only the Himalayas"]