Python `请求.get`object,试图将`xml`解析为`pandas.DataFrame`
我已成功创建了一个Python `请求.get`object,试图将`xml`解析为`pandas.DataFrame`,python,xml,pandas,Python,Xml,Pandas,我已成功创建了一个请求。从中获取(url)对象,使用以下方法创建: import requests In [103]: socket = requests.get(''http://www.zillow.com/...neighborhood') In [112]: root.tag Out[112]: '{http://www.zillow.com/static/xsd/RegionChildren.xsd}regionchildren' In [114]: for child in r
请求。从中获取(url)
对象,使用以下方法创建:
import requests
In [103]: socket = requests.get(''http://www.zillow.com/...neighborhood')
In [112]: root.tag
Out[112]: '{http://www.zillow.com/static/xsd/RegionChildren.xsd}regionchildren'
In [114]: for child in root:
print child.tag, child.attrib
.....:
request {}
message {}
response {}
当我检查socket
对象中的内容时,我得到了很多内容,我将从一开始就分享其中的内容:
In [106]: socket.content
Out[106]: '<?xml version="1.0" encoding="utf-8"?><RegionChildren:regionchildren
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zillow.com/static/xsd/RegionChildren.xsd
http://www.zillowstatic.com/vstatic/479fdf9/static/xsd/RegionChildren.xsd"
xmlns:RegionChildren="http://www.zillow.com/static/xsd/RegionChildren.xsd"><request>
<state>wa</state><city>seattle</city><childtype>neighborhood</childtype></request>
<message><text>Request successfully processed</text><code>0</code></message><response>
<region><id>16037</id><latitude>47.559364</latitude><longitude>-122.313752</longitude>
</region><subregiontype>neighborhood</subregiontype><list><count>107</count><region>
<id>250206</id><name>Capitol Hill</name><zindex currency="USD">398000</zindex>
<url>http://www.zillow.com/local-info/WA-Seattle/Capitol-Hill/r_250206/</url>...'
我去过,在那里他们演示了如何解析xml
文件。看起来(根据文档),这应该是解析字符串的适当方法:
import lxml.etree.ElementTree as ET
In [107]: root = ET.fromstring(socket.content)
但是,我仍然无法使用教程中提供的任何后续步骤访问任何标记元素(
,
,
,等等),例如,我得到以下结果:
import requests
In [103]: socket = requests.get(''http://www.zillow.com/...neighborhood')
In [112]: root.tag
Out[112]: '{http://www.zillow.com/static/xsd/RegionChildren.xsd}regionchildren'
In [114]: for child in root:
print child.tag, child.attrib
.....:
request {}
message {}
response {}
我希望能够迭代所有不同的标记元素,将它们放入
dict
或pandas.DataFrame
,但我无法获得实际访问元素的第一步。最后,我能够通过使用。具体如下:
import requests
In [103]: socket = requests.get(''http://www.zillow.com/...neighborhood')
In [112]: root.tag
Out[112]: '{http://www.zillow.com/static/xsd/RegionChildren.xsd}regionchildren'
In [114]: for child in root:
print child.tag, child.attrib
.....:
request {}
message {}
response {}
步骤1:
按照特别建议,将数据加载到bs4
对象中
步骤2:
查看一下xml
数据的混乱情况,以确定它的结构
[In] 11: f = open('pretty_xml.xml', 'w')
[In] 12: f.writelines(soup.prettify())
[In] 13: f.close()
步骤3:
开始提取
仔细查看数据后,使用bs4.findChildren()
功能将每个节点提取到列表的单元格中。例如:
[In] 14: soup_list = soup.findChildren()
然后(假设每个列表元素的标记
相同,或者与我的情况基本相似),使用元素创建(索引,值)
配对,执行如下操作:
[In] 15: d = {}
[In] 16: for i, element in enumerate(soup_list):
...: index = map(lambda x: x.name, element.findChildren())
...: vals = map(lambda x: unicode(x.text), element.findChildren())
...: d[i] = pandas.Series(vals, index = index)
然后,您就可以创建一个
pandas.DataFrame
来传递系列的dict
你怎么能把所有的标签放在一张纸上?必须有多个具有相似名称的标记..我尝试在我的XML行中给出上面的所有标记,很抱歉显示不好。他们是:。有100多个条目,每个条目都有这些标签(同上)。很好的解释。我知道这是很久以前的事了,但是你是如何做最后一次传递系列的记录的?我假设你说的是创建最终的数据帧(正确吗?),在这种情况下,它就是,df=pandas.DataFrame(d).transpose()
是的,我正在处理一个类似的问题,但无法回避重复列问题。我会在数据帧内转置它吗?aka'df=pd.DataFrame(d.transpose())?