Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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中解析的HREF_Python_Parsing_Scrapy - Fatal编程技术网

Python 将项目链接到scrapy中解析的HREF

Python 将项目链接到scrapy中解析的HREF,python,parsing,scrapy,Python,Parsing,Scrapy,设置 我用刮痧刮房屋广告 根据,我获得了一个包含链接到单个广告的HREF的列表。通过for循环,HREF被发送到第二个解析器函数,以获得每个广告的外壳特征 def parse(self, response): # for href in list with hrefs for href in response.xpath( '//*[@id]/@href', ).extract()[1:-1]:

设置

我用刮痧刮房屋广告

根据,我获得了一个包含链接到单个广告的HREF的列表。通过for循环,HREF被发送到第二个解析器函数,以获得每个广告的外壳特征

def parse(self, response):
        # for href in list with hrefs
        for href in response.xpath(
                '//*[@id]/@href',
                ).extract()[1:-1]:
            yield scrapy.Request(response.urljoin(href),
                     callback=self.parse_ad)

def parse_ad(self, response):
# here follows code to obtain housing characteristics per ad

    yield {'char1': char1,
           'char2': char2,}
这个很好用


问题

除了hrefs,我还使用

response.xpath('//*[@id]/div[1]/div/div[1]/div[1]/div[2]/meta').extract() 
最终我希望

    yield {'char1': char1,
           'char2': char2,
           'postal code': postal_code}
但我不知道该怎么做

  • 让python同时选择
    href
    及其对应的
    邮政编码
  • 邮政编码
    带到
    parse_ad()下的
    yield
    函数
  • 我该怎么办

    要将事情从一个回调方法“继续”到另一个回调方法,请使用:


    1.是否要使用函数
    解析中提取的
    href
    ?2.在一个函数中可以有几个
    yield
    调用来“返回”几个dict。
    def parse(self, response):
        for search_result in response.css(".room-tile.rowSearchResultRoom"):
            postal_code = search_result.css("meta[itemprop=postalCode]::attr(content)").extract_first()
            href = search_result.xpath("@href").extract_first()
    
            yield scrapy.Request(response.urljoin(href),
                                 meta={'postal_code': postal_code},
                                 callback=self.parse_ad)
    
    def parse_ad(self, response):
        postal_code = response.meta['postal_code']
    
        # get char1 and char2..
    
        yield {'char1': char1,
               'char2': char2,
               'postal_code': postal_code}