我正在解析一个html文件,我想找到它所说的"Smaller Reporting Company"文件的一部分,并且它旁边有一个"X"或Checkbox,或者它没有't. The checkbox is typically done with the Wingdings font or an ascii code. In the HTML below you' ll看到它旁边有一个 þ
.
我没有问题显示正则表达式搜索文本的结果,但我无法进入下一步并寻找一个复选框 .
我将使用它来解析许多不同的html文件,这些文件并不都遵循相同的格式,但是大多数文件将使用表格和ascii文本,就像这个例子一样 .
这是HTML代码:
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY>
<DIV align="left">Indicate by check mark whether the registrant is a large accelerated filer, an accelerated filer, a non-accelerated filer, or a smaller reporting company. See the definitions of “large accelerated filer,” “accelerated filer” and “smaller reporting company”. (Check one):
</DIV>
<DIV align="center">
<TABLE style="font-size: 10pt" cellspacing="0" border="0" cellpadding="0" width="100%">
<!-- Begin Table Head -->
<TR valign="bottom">
<TD width="22%"> </TD>
<TD width="3%"> </TD>
<TD width="22%"> </TD>
<TD width="3%"> </TD>
<TD width="22%"> </TD>
<TD width="3%"> </TD>
<TD width="22%"> </TD>
</TR>
<TR></TR>
<!-- End Table Head -->
<!-- Begin Table Body -->
<TR valign="bottom">
<TD align="center" valign="top"><FONT style="white-space: nowrap"> Large accelerated filer <FONT style="font-family: Wingdings">o</FONT></FONT>
</TD>
<TD> </TD>
<TD align="center" valign="top"><FONT style="white-space: nowrap">Accelerated filer <FONT style="font-family: Wingdings">o</FONT></FONT>
</TD>
<TD> </TD>
<TD align="center" valign="top"><FONT style="white-space: nowrap"> Non-accelerated filer <FONT style="font-family: Wingdings">o</FONT> </FONT>
<FONT style="white-space: nowrap">(Do not check if a smaller reporting company)</FONT>
</TD>
<TD> </TD>
<TD align="center" valign="top"><FONT style="white-space: nowrap"> Smaller reporting company <FONT style="font-family: Wingdings">þ</FONT></FONT></TD>
</TR>
<!-- End Table Body -->
</TABLE>
</DIV></BODY></HTML>
这是我的Python代码:
import os, sys, string, re
from BeautifulSoup import BeautifulSoup
rawDataFile = "testfile1.html"
f = open(rawDataFile)
soup = BeautifulSoup(f)
f.close()
search = soup.findAll(text=re.compile('[sS]maller.*[rR]eporting.*[cC]ompany'))
print search
问题:如何将其设置为依赖于第一次搜索的第二次搜索?因此,当我找到“较小的报告公司”时,我可以搜索下几行,看看是否有ascii代码?我一直在浏览汤文档 . 我试图找到findNext但我无法让它工作 .
3 回答
如果您知道翼型角色的位置不会改变,您可以使用
.next
.或者你可以上去,然后从那里
find
:或者你可以反过来做:
这假设你知道你正在寻找的翘曲的caharcters .
最后一个策略还有一个额外的好处,就是过滤掉你的正则表达式所捕获的其他垃圾,我想你只是在正确的列表上工作,所以你可以根据自己的喜好仔细阅读
if
.您可以尝试遍历结构并检查内部标记内的值或检查外部标记中的值 . 我不记得如何做到这一点,我最终使用lxml,但我认为bsoup可能能做到这一点 .
如果你不能得到bsoup来检查lxml . 根据您的工作情况,它可能更快 . 它还有使用bsoup和lxml的钩子 .
lxml
有一个宽容的HTML解析器 . 您不需要bsoup(现在已被其作者弃用),您应该避免使用正则表达式来解析HTML .这是您正在寻找的第一个粗略切口:
这会产生: