【发布时间】:2015-03-19 10:34:03
【问题描述】:
使用 BeautifulSoup for Python 来解析网页(不幸的是,这主要是在表格中编写的)。
这是我正在尝试使用的内容的摘录
<tr>
<td colspan="4">
<div class="shortmenucats">
<span style="color: ">
-- Fresh Baked Pastries --
</span>
</div>
</td>
</tr>
<tr>
<td width="80%" valign="top">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<div class="shortmenurecipes">
<span style="color: #000000"> Chocolate Doughnut Holes </span>
</div>
</td>
</tr>
</tbody>
</table>
</td>
<td width="5%" valign="top"></td>
<td width="10%" valign="top" align="right" colspan="1">
<div class="shortmenuprices">
<span style="color: #000000"></span>
</div>
</td>
<td width="5%" valign="top" colspan="1">
</td>
</tr>
这些是表中的两行,其中包含 10 行,它们交替出现(td 中的 div、td 中的表、td 中的 div、td 中的表等)。
我正在使用 BeautifulSoup 在父表上调用 find_all 并且由于嵌套表中的嵌套标签,它会返回每隔一行的重复项。
我首先做了一个table.find_all('td', recursive=False),但这根本没有返回任何s。
如果我在父表上调用findChildren(),我会得到一个包含一个结果的列表,但它包含结果中的所有子项。
我做错了吗?我不知道如何解决这个问题。
如果您想要我从中解析的实际网站,请点击此处: http://138.23.12.141/foodpro/shortmenu.asp?sName=University+of+California%2C+Riverside+Dining+Services&locationNum=02&locationName=Lothian+Residential+Restaurant&naFlag=1
它的代码非常混乱。我只是想解析它。
任何帮助将不胜感激。即使这只是删除重复项的一种方法。
谢谢。
【问题讨论】:
-
那么您到底想提取哪些部分?
spans 中的文字?或者,还有什么?
标签: python html web-scraping beautifulsoup html-parsing