Python 将项目链接到scrapy中解析的HREF
设置 我用刮痧刮房屋广告 根据,我获得了一个包含链接到单个广告的HREF的列表。通过for循环,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]:
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}
但我不知道该怎么做
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}