<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"InstrumentID":85,"BuyPrice":24677.0,"SellPrice":24671.0,"HighPrice":24671.0,"LowPrice":24212.0,"ChangePercent":2.1,"ChangePercentText":"2.10%","UsersBuyPercentage":56.0,"UsersSellPercentage":44.0,"IsValid":true}
</string>
我需要用Scrapy提取BuyPrice,SellPrice,但我不知道如何。有人可以帮忙吗?
答案 0 :(得分:1)
看起来你在xml中有json,所以提取数据将是一个由两部分组成的任务:
json
模块如何完成此操作的示例(在此使用scrapy shell
):
>>> import json
>>> sel = scrapy.Selector(text='''<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
... {"InstrumentID":85,"BuyPrice":24677.0,"SellPrice":24671.0,"HighPrice":24671.0,"LowPrice":24212.0,"ChangePercent":2.1,"ChangePercentText":"2.10%","UsersBuyPercentage":56.0,"UsersSellPercentage":44.0,"IsValid":true}
... </string>''')
>>> sel.remove_namespaces()
>>> json.loads(sel.xpath('//string/text()').get())
{'InstrumentID': 85, 'BuyPrice': 24677.0, 'SellPrice': 24671.0, 'HighPrice': 24671.0, 'LowPrice': 24212.0, 'ChangePercent': 2.1, 'ChangePercentText': '2.10%', 'UsersBuyPercentage': 56.0, 'UsersSellPercentage': 44.0, 'IsValid': True}