我正在抓取一个站点,我想生成一个xml,其中每个区域都在它所属的国家范围内。你知道吗
def parse(self, response):
#here i parse the country names
country_names = response.xpath('//some countries/text()').extract()
for name_of_country in countries :
yield {"Country": name_of_country }
yield Request(country_url, callback=self.parse_regions)
def parse_regions(self, response):
#here i parse the regions of each country
regions= response.xpath('//some regions/text()').extract()
for region in regions
yield {"Region": region }
现在XML显示为:
<Country1></Country1>
<Country2></Country2>
<Region>Region1</Region>
<Region>Region2</Region>
<Region>Region3</Region>
<Region>Region1</Region>
<Region>Region2</Region>
<Region>Region3</Region>
我希望XML显示如下:
<Country1>
<Region>Region1</Region>
<Region>Region2</Region>
<Region>Region3</Region>
</Country1>
<Country2>
<Region>Region1</Region>
<Region>Region2</Region>
<Region>Region3</Region>
</Country2>
我从未使用过
XML
,但您可以将Country
发送到第二个请求(使用meta=
),然后在parse_region
中创建一个包含所有数据的元素。你知道吗我使用http://quotes.toscrape.com获得一些标记,并使用as
Country
,然后发送给parse_region
,后者获得所有区域并只产生一个元素。你知道吗解决方案不是完美的,因为它会
也许您可以使用自己的导出器将
<value>
更改为<region>
并删除<item>
-请参见Formatting Scrapy's output to XML完整工作示例
相关问题 更多 >
编程相关推荐