Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scrapy只返回第一个结果_Python_Scrapy - Fatal编程技术网

Python Scrapy只返回第一个结果

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

我试图从gelbeseiten.de(德国的黄页)中获取数据

因此,我得到了15套始终相同的地址,但我认为应该是15个不同的地址


非常感谢您的帮助。

您使用的是绝对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,
      }