python可以';<;a>;标签

python可以';<;a>;标签,python,python-2.7,xpath,scrapy,Python,Python 2.7,Xpath,Scrapy,请看这张来自firebug的图片 我想在标记中进行测试。我用了这个: def parse(self, response): sel = Selector(response) sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]') cars = [] for site in sites: car = CarItem()

请看这张来自firebug的图片

我想在
标记中进行测试。我用了这个:

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle']=xpath('.//div[@class="block item-title"]/h3/span[@class="title"]/a/text()').extract()
        cars.append(car)
    return cars
我想我使用了正确的xpath。但似乎没有,因为我得到的结果是空的


有什么帮助吗?

以下是OP的评论:

这可能是您的目标:

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle']=site.xpath('.//div[@class="block item-title"]/h3/span[@class="title"]/a/text()').extract()
        cars.append(car)
    return cars
或者,我看到您使用的是最新的Scrapy版本,所以您可能希望尝试CSS选择器,它通常使选择器表达式更易于阅读和维护

在你的情况下,你可以使用

def parse(self, response):
    sel = Selector(response)
    sites = sel.css('div.paid-featured-item div.listing-item')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle'] = site.css('div.item-title h3 span.title a::text').extract()
        cars.append(car)
    return cars

请注意,
a::text
语法是CSS选择器的一个粗略扩展

在OP的评论之后:

这可能是您的目标:

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle']=site.xpath('.//div[@class="block item-title"]/h3/span[@class="title"]/a/text()').extract()
        cars.append(car)
    return cars
或者,我看到您使用的是最新的Scrapy版本,所以您可能希望尝试CSS选择器,它通常使选择器表达式更易于阅读和维护

在你的情况下,你可以使用

def parse(self, response):
    sel = Selector(response)
    sites = sel.css('div.paid-featured-item div.listing-item')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle'] = site.css('div.item-title h3 span.title a::text').extract()
        cars.append(car)
    return cars

请注意,
a::text
语法是CSS选择器的一个粗略扩展

在OP的评论之后:

这可能是您的目标:

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle']=site.xpath('.//div[@class="block item-title"]/h3/span[@class="title"]/a/text()').extract()
        cars.append(car)
    return cars
或者,我看到您使用的是最新的Scrapy版本,所以您可能希望尝试CSS选择器,它通常使选择器表达式更易于阅读和维护

在你的情况下,你可以使用

def parse(self, response):
    sel = Selector(response)
    sites = sel.css('div.paid-featured-item div.listing-item')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle'] = site.css('div.item-title h3 span.title a::text').extract()
        cars.append(car)
    return cars

请注意,
a::text
语法是CSS选择器的一个粗略扩展

在OP的评论之后:

这可能是您的目标:

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle']=site.xpath('.//div[@class="block item-title"]/h3/span[@class="title"]/a/text()').extract()
        cars.append(car)
    return cars
或者,我看到您使用的是最新的Scrapy版本,所以您可能希望尝试CSS选择器,它通常使选择器表达式更易于阅读和维护

在你的情况下,你可以使用

def parse(self, response):
    sel = Selector(response)
    sites = sel.css('div.paid-featured-item div.listing-item')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle'] = site.css('div.item-title h3 span.title a::text').extract()
        cars.append(car)
    return cars


请注意,
a::text
语法是CSS选择器的一个粗略扩展

不知道它是否是打字错误,但您应该有类似于
car['ATitle']=site.xpath(…
,而不是
car['ATitle']=xpath的内容(…
@pault。我不知道。使用空格不正确吗?@pault。但是我只是尝试使用空格,仍然不正确result@pault.请写下答案接受它。下次在firebug中解决我的问题时,最好右键单击并选择“复制Html”拍照可能不是最好的主意…不知道这是否是打字错误,但你应该这样做使用类似于
car['ATitle']=site.xpath(…
)的东西,而不是
car['ATitle']=xpath(…
@pault。我不知道。使用空格不正确吗?@pault。但是我只是尝试使用空格,仍然不正确result@pault.请写下答案接受它。下次在firebug中解决我的问题时,最好右键单击并选择“复制Html”拍照可能不是最好的主意…不知道这是否是打字错误,但你应该这样做使用类似于
car['ATitle']=site.xpath(…
)的东西,而不是
car['ATitle']=xpath(…
@pault。我不知道。使用空格不正确吗?@pault。但是我只是尝试使用空格,仍然不正确result@pault.请写下答案接受它。下次在firebug中解决我的问题时,最好右键单击并选择“复制Html”拍照可能不是最好的主意…不知道这是否是打字错误,但你应该这样做使用类似于
car['ATitle']=site.xpath(…
)的东西,而不是
car['ATitle']=xpath(…
@pault。我不知道。使用空格不正确吗?@pault。但是我只是尝试使用空格,仍然不正确result@pault.请写下答案接受它。下次在firebug中解决我的问题最好右键单击并选择“复制Html”拍照可能不是最好的主意。。。