使用Scrapy从字符串中提取数据

2024-09-29 19:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

<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>

我需要提取买价,卖价与刮,但我不知道如何。有人能帮忙吗?你知道吗


Tags: comhttpstringserializationschemasmicrosoftxmlnssellprice
1条回答
网友
1楼 · 发布于 2024-09-29 19:30:36

看起来xml中有json,因此提取数据将分为两部分:

  1. 提取json字符串
  2. 使用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}

相关问题 更多 >

    热门问题