Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup解析特定文本_Python_Regex_Html Parsing_Beautifulsoup - Fatal编程技术网

Python BeautifulSoup解析特定文本

Python BeautifulSoup解析特定文本,python,regex,html-parsing,beautifulsoup,Python,Regex,Html Parsing,Beautifulsoup,我正在解析一个html文件,我想找到文件中显示“较小的报告公司”的部分,该部分旁边有一个“X”或复选框,或者没有。该复选框通常使用Wingdings字体或ascii代码完成。在下面的HTML中,您将看到它有一个þ在它旁边的wingdings中 我在显示文本的正则表达式搜索结果时没有问题,但在下一步查找复选框时遇到了问题 我将使用它来解析许多不同的html文件,这些文件并不都遵循相同的格式,但它们中的大多数将使用一个表和ascii文本,如本例所示 以下是HTML代码: <HTML&g

我正在解析一个html文件,我想找到文件中显示“较小的报告公司”的部分,该部分旁边有一个“X”或复选框,或者没有。该复选框通常使用Wingdings字体或ascii代码完成。在下面的HTML中,您将看到它有一个
þ在它旁边的wingdings中

我在显示文本的正则表达式搜索结果时没有问题,但在下一步查找复选框时遇到了问题

我将使用它来解析许多不同的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 &#147;large accelerated filer,&#148; &#147;accelerated filer&#148; and &#147;smaller reporting company&#148;. (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%">&nbsp;</TD>
    <TD width="3%">&nbsp;</TD>
    <TD width="22%">&nbsp;</TD>
    <TD width="3%">&nbsp;</TD>
    <TD width="22%">&nbsp;</TD>
    <TD width="3%">&nbsp;</TD>
    <TD width="22%">&nbsp;</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">&#111;</FONT></FONT>
    </TD>
    <TD>&nbsp;</TD>
    <TD align="center" valign="top"><FONT style="white-space: nowrap">Accelerated filer <FONT style="font-family: Wingdings">&#111;</FONT></FONT>
    </TD>
    <TD>&nbsp;</TD>
    <TD align="center" valign="top"><FONT style="white-space: nowrap"> Non-accelerated filer <FONT style="font-family: Wingdings">&#111;</FONT> </FONT>
    <FONT style="white-space: nowrap">(Do not check if a smaller reporting company)</FONT>
    </TD>
    <TD>&nbsp;</TD>
    <TD align="center" valign="top"><FONT style="white-space: nowrap"> Smaller reporting company <FONT style="font-family: Wingdings">&#254;</FONT></FONT></TD>
</TR>
<!-- End Table Body -->
</TABLE>
</DIV></BODY></HTML>
问题:
我如何将其设置为具有依赖于第一次搜索的第二次搜索?所以,当我找到“较小的报告公司”时,我可以搜索接下来的几行,看看是否有ascii码?我一直在看汤医生。我尝试了find和findNext,但未能使其正常工作。

您可以尝试遍历结构,检查内部标记中的值,或检查外部标记中的值。我一时记不起怎么做了,最后我用了lxml来做这个,但我认为bsoup可能能够做到这一点


如果您无法让bsoup执行此操作,请查看lxml。它可能会更快,这取决于你在做什么。它还具有钩子,用于将bsoup与lxml一起使用。

如果您知道wingding字符的位置不会改变,则可以使用
。下一步

>>> nodes = soup.findAll(text=re.compile('[sS]maller.*[rR]eporting.*[cC]ompany'))
>>> nodes[-1].next.next  # last item in list is the only good one... kinda crap
u'&#254;'
或者你可以上去,然后从那里找到:

>>> nodes[-1].parent.find('font',style="font-family: Wingdings").next
u'&#254;'
或者你也可以反过来做:

>>> soup.findAll(text='&#254;')[0].previous.previous
u' Smaller reporting company '
这假设你知道你要找的翼龙


最后一个策略还有一个额外的好处,那就是过滤掉你的正则表达式捕捉到的其他垃圾,我想这是你并不真正想要的;然后,您可以循环浏览结果,知道您只处理了正确的列表,这样您就可以根据自己的喜好阅读

lxml
有一个宽容的HTML解析器。您不需要bsoup(它现在已被作者弃用),并且应该避免使用正则表达式来解析HTML

以下是您要寻找的第一个粗略切割:

guff = """\
<HTML>
<HEAD><TITLE></TITLE></HEAD>
[snip]
</DIV></BODY></HTML>
"""
from lxml.html import fromstring
doc = fromstring(guff)
for td_el in doc.iter('td'):
    font_els = list(td_el.iter('font'))
    if not font_els: continue
    print
    for el in font_els:
        print (el.text, el.attrib)

我打赌你应该更正“旁边有一个“X”或复选框”改为“旁边有一个“X”复选框”,而你没有。这使我感到不安,也使我无法理解你的问题。你不在乎被人理解吗?“在下面的HTML中,你会看到它旁边有一个þIn wingdings。”??哪里??什么叫“ascii码”?是o;和þ-一片混乱。在显微镜下仔细观察,“它旁边的wingdings中的anþ”不是
拉丁文小写字母P
,而是
þ
aka
拉丁文小写字母THORN
。任何想知道这个角色有什么关联的人都可以阅读。。。111是一个空框,254是一个打勾的框,我应该在
þ周围加上回勾最初过帐时的代码。我更正了原来的帖子。lxml的问题是对libxml的依赖性,而libxml并不总是可用的——例如,你根本不能在Jython中使用它。BS的美妙之处在于它只是纯Python的一个文件。
guff = """\
<HTML>
<HEAD><TITLE></TITLE></HEAD>
[snip]
</DIV></BODY></HTML>
"""
from lxml.html import fromstring
doc = fromstring(guff)
for td_el in doc.iter('td'):
    font_els = list(td_el.iter('font'))
    if not font_els: continue
    print
    for el in font_els:
        print (el.text, el.attrib)
(' Large accelerated filer ', {'style': 'white-space: nowrap'})
('o', {'style': 'font-family: Wingdings'})

('Accelerated filer ', {'style': 'white-space: nowrap'})
('o', {'style': 'font-family: Wingdings'})

(' Non-accelerated filer ', {'style': 'white-space: nowrap'})
('o', {'style': 'font-family: Wingdings'})
('(Do not check if a smaller reporting company)', {'style': 'white-space: nowrap
'})

(' Smaller reporting company ', {'style': 'white-space: nowrap'})
(u'\xfe', {'style': 'font-family: Wingdings'})