好吧所以我有这个HTML文件,其中包含许多div标签和表标签的数据。 div标签包含与其他div标签部分相关的id,但在每个div标签部分之后是包含我需要的数据的表部分。我希望能够获取这个HTML文件并创建数组,列表,dicts等...某种结构,以便我可以轻松搜索相关信息并从中提取我需要的内容。
HTML文件中的whats示例。
<DIV class="info"> <A name="bc968f9fa2db71455f50e0c13ce50e871fS7f0e"
id="bc968f9fa2db71455f50e0c13ce50e871fS7f0e">
<B>WORKSPACE_WEBAPP</B> (WORKSPACE_WEBAPP)<BR/> <B>Object ID:
</B> bc968f9fa2db71455f50e0c13ce50e871fS7f0e<BR/> <B>Last
Modified Date : </B> 26-Sep-13 10:41:13<BR/>
<B>Properties:</B><BR/> </DIV>
<TABLE class="properties"> <TR class="header"><TH>Property
Name</TH><TH>Property Value</TH></TR>
<TR><TD>serverName</TD><TD>FoundationServices0</TD></TR>
<TR><TD>context</TD><TD>workspace</TD></TR>
<TR><TD>isCompact</TD><TD>false</TD></TR>
<TR><TD>AppServer</TD><TD>WebLogic 10</TD></TR>
<TR><TD>port</TD><TD>28080</TD></TR>
<TR><TD>maintVersion</TD><TD>11.1.2.2.0.66</TD></TR>
<TR><TD>version</TD><TD>11.1.2.0</TD></TR>
<TR><TD>SSL_Port</TD><TD>28443</TD></TR>
<TR><TD>instance_home</TD><TD>/essdev1/app/oracle/Middleware/user_projects/epmsystem1</TD></TR>
<TR><TD>configureBPMUIStaticContent</TD><TD>true</TD></TR>
<TR><TD>validationContext</TD><TD>workspace/status</TD></TR> </TABLE>
所以我希望能够为这些div部分创建一个数组,并且还包含该表中的区域以及该数组中的区域。我无法绕过最好的方式来解决问题。我知道答案可能包含使用BeautifulSoup来解析标签。由于没有其他方法将表格部分与div部分联系起来,我相信我必须一次加载一行文件并以此方式处理它,除非有更简单的方法吗?任何想法都会非常有用。
答案 0 :(得分:2)
使用BeautifulSoup
基本解决方案是使用加入,美化和拆分。基本的想法是转换 文字和分开 感兴趣的部分
from bs4 import BeautifulSoup
soup = BeautifulSoup(''.join(text))
for i in soup.prettify().split('<!--Persontype-->')[1].split('<strong>'):
print '<strong>' + ''.join(i)
text= '''
<div class="clearfix">
<!--# of ppl associated with place-->
This is some kind of buzzword:<br />
<br />
<!--Persontype-->
<strong>Hey</strong> All <br />
Something text here <br />
About Something
<br />
Mobile Version <br />
<br />
<strong>MObile</strong> Nokia <br />
Try to implement here <br />
Simple
<br />
hey Thanks <br />
O/P is :
答案 1 :(得分:1)
首先,我需要重申你的问题。该示例显示了div
标记,其中包含A
标记。 A
标记具有您要用作查找下表的键的ID。 div
代码后跟table
。表的每一行都包含与上一个A
中标识的对象关联的名称 - 值对。
您的网页上有多个div
标记,每个标记都在前一段中描述。
您想生成一些数据结构以方便地访问表数据并将其与命名对象相关联吗?
我有这个权利吗?
你预言的答案是使用BeautifulSoup。我们将创建一个以id
属性为键的字典。字典中的每个值本身都是一个字典,由&#34;属性名称&#34;键入。在表中。
from bs4 import BeautifulSoup
from pprint import pprint
result = {}
soup = BeautifulSoup(page)
divs = soup.find_all("div", {"class":"info"})
for div in divs:
name = div.find("a")["id"]
table = div.find_next("table", {"class":"properties"})
rows = table.find_all("tr", {"class":None})
rowd = {}
for row in rows:
cells = row.find_all("td")
rowd[cells[0].text] = cells[1].text
result[name] = rowd
pprint (result)
或者,如果您更喜欢单词理解(就像我一样):
result = {
div.find("a")["id"]: {
cells[0].text : cells[1].text
for row in table.find_all("tr", {"class":None})
for cells in [row.find_all("td")]
}
for div in soup.find_all("div", {"class":"info"})
for table in [div.find_next("table", {"class":"properties"})]
}
pprint(result)
当您指向示例数据时,会产生:
{'bc968f9fa2db71455f50e0c13ce50e871fS7f0e': {u'AppServer': u'WebLogic 10',
u'SSL_Port': u'28443',
u'configureBPMUIStaticContent': u'true',
u'context': u'workspace',
u'instance_home': u'/essdev1/app/oracle/Middleware/user_projects/epmsystem1',
u'isCompact': u'false',
u'maintVersion': u'11.1.2.2.0.66',
u'port': u'28080',
u'serverName': u'FoundationServices0',
u'validationContext': u'workspace/status',
u'version': u'11.1.2.0'}}
要使用数据结构,只需按照词典进行操作即可。例如:
print result["bc968f9fa2db71455f50e0c13ce50e871fS7f0e"]["serverName"]