Python Scrapy只返回第一个结果
我试图从gelbeseiten.de(德国的黄页)中获取数据 因此,我得到了15套始终相同的地址,但我认为应该是15个不同的地址Python Scrapy只返回第一个结果,python,scrapy,Python,Scrapy,我试图从gelbeseiten.de(德国的黄页)中获取数据 因此,我得到了15套始终相同的地址,但我认为应该是15个不同的地址 非常感谢您的帮助。您使用的是绝对xpath表达式: address.xpath('//span[@itemprop=“streetAddress”]///text()) 而应使用相对地址(注意表达式中的前导点): address.xpath('.//span[@itemprop=“streetAddress”]//text()) # -*- coding: utf-8
非常感谢您的帮助。您使用的是绝对xpath表达式:
address.xpath('//span[@itemprop=“streetAddress”]///text())
而应使用相对地址(注意表达式中的前导点):
address.xpath('.//span[@itemprop=“streetAddress”]//text())
# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import CrawlSpider
from scrapy.http import Request
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
class GelbeseitenSpider(scrapy.Spider):
name = "gelbeseiten"
allowed_domains = ["http://www.gelbeseiten.de"]
start_urls = ['http://www.gelbeseiten.de/zoohandlungen/s1/alphabetisch']
def parse(self, response):
for adress in response.css('article'):
#Strasse
strasse = adress.xpath('//span[@itemprop="streetAddress"]//text()').extract_first()
#Name
name = adress.xpath('//span[@itemprop="name"]//text()').extract_first()
#PLZ
plz = adress.xpath('//span[@itemprop="postalCode"]//text()').extract_first()
#Stadt
stadt = adress.xpath('//span[@itemprop="addressLocality"]//text()').extract_first()
yield {
'name': name,
'strasse': strasse,
'plz': plz,
'stadt': stadt,
}