用Python从html文件中收集信息

2024-09-30 14:27:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我是一个编程新手,对Python和html的知识有限!我要做的是运行一个web爬行python程序,从一些htmls中获取一些特定的名称。你知道吗

假设在这个html代码中有一些url:

<TR>
<TD VALIGN="top"> <P STYLE="margin-top:0px;margin-bottom:0px"><FONT FACE="Times New Roman"           SIZE="2">/s/ ROBERT F. MANGANO</FONT></P><HR WIDTH="91%" SIZE="1" NOSHADE COLOR="#000000"  ALIGN="left"></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="top" ROWSPAN="2"> <P STYLE="margin-top:0px;margin-bottom:0px"><FONT FACE="Times New   Roman" SIZE="2">President, Chief Executive Officer and Director</FONT></P> <P STYLE="margin- top:0px;margin-bottom:1px"><FONT FACE="Times New Roman"
SIZE="2">(Principal Executive Officer)</FONT></P></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;</FONT></TD>
<TD VALIGN="top" ROWSPAN="2" ALIGN="center"><FONT FACE="Times New Roman" SIZE="2">March 24,  2005</FONT></TD></TR>

如下所示:

/s/ ROBERT F. MANGANO

   President, Chief Executive Officer and Director (Principal Executive Officer)   March 24, 2005

我想提取这个人的名字和头衔。所以,在python中,我写了以下内容:

def htmlParser(self):
    pageTree = html.fromstring(self.pageContent)
    print "page parsed!"
    tdTexts =  pageTree.xpath("//td/descendant::*/text()")
    cleanTexts = [eachText.strip() for eachText in tdTexts if eachText.strip()]
    for i in range(1,len(cleanTexts)):
        if ('/s/' in cleanTexts[i] and (i+1) < len(cleanTexts)):
            title = []
            title = [cleanTexts [i+1] for eachKeyword in titleKeywords if eachKeyword in cleanTexts [i+1].lower()]
            if (title):
                print title
                self.boards.append([self.pageURL,cleanTexts[i].replace('/s/',''),cleanTexts [i+1]])
                print self.boards
            elif (i+2) < len(cleanTexts):
                title = [cleanTexts [i+2] for eachKeyword in titleKeywords if eachKeyword in cleanTexts [i+2].lower()]
                if (title):
                    self.boards.append([self.pageURL,cleanTexts[i].replace('/s/',''),cleanTexts [i+2]])

我发现的唯一一个模式是,在表单中重复出现的,所以我要坚持。上面的代码非常适合我。给我这个:

总裁兼首席执行官罗伯特曼加诺

现在,我面对的是另一种形式:

</TR>
<TR VALIGN="TOP">
<TD WIDTH="40%" ALIGN="CENTER" VALIGN="CENTER"><FONT SIZE=2>/s/&nbsp;&nbsp;</FONT><FONT     SIZE=2>JONATHAN C. COON</FONT><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT><HR NOSHADE>    <FONT SIZE=2> Jonathan C. Coon</FONT></TD>
<TD WIDTH="3%" VALIGN="CENTER"><FONT SIZE=2>&nbsp;</FONT></TD>
<TD WIDTH="58%" VALIGN="CENTER"><FONT SIZE=2>Chief Executive Officer and Director (principal    executive officer)</FONT></TD>
 </TR>

看起来像:

^{2}$

它通常是相同的,但有这种“和字体”的东西 在/s/和名称之间(在前一种形式中,/s/后跟名称) 我不知道有多少html,所以这就是我发现这两个html之间的区别。如果有什么不同的,请告诉我。你知道吗

我认为我的代码对这类代码也能起同样的作用,因为我使用“//td/descendant::*/text()”来消除所有的html标记和内容,只需查看单词。但是,当我运行后一个html的代码时,它会给出: ;;首席执行官

如您所见,在本例中无法捕获名称。我不知道应该如何修改代码来覆盖这两种情况,而且由于我对html的了解很少,我无法高效地搜索来解决这个问题。你知道吗

有没有人能帮我修改代码,以便捕获这两个名字?你知道吗

非常感谢。你知道吗

附言:对不起,如果我解释得不对的话。我说过,我不是职业选手!如果我的问题缺少解释,请告诉我


Tags: 代码inmarginselfsizeiftitletop
1条回答
网友
1楼 · 发布于 2024-09-30 14:27:57

使用beautifulSoup解析html:

from bs4 import BeautifulSoup

html = """
<TR>
<TD VALIGN="top"> <P STYLE="margin-top:0px;margin-bottom:0px"><FONT FACE="Times New Roman"           SIZE="2">/s/ ROBERT F. MANGANO</FONT></P><HR WIDTH="91%" SIZE="1" NOSHADE COLOR="#000000"  ALIGN="left"></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="top" ROWSPAN="2"> <P STYLE="margin-top:0px;margin-bottom:0px"><FONT FACE="Times New   Roman" SIZE="2">President, Chief Executive Officer and Director</FONT></P> <P STYLE="margin- top:0px;margin-bottom:1px"><FONT FACE="Times New Roman"
SIZE="2">(Principal Executive Officer)</FONT></P></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;</FONT></TD>
<TD VALIGN="top" ROWSPAN="2" ALIGN="center"><FONT FACE="Times New Roman" SIZE="2">March 24,  2005</FONT></TD></TR>
"""

soup = BeautifulSoup(html)

print("\n".join([x.text.strip() for x in soup.find_all("td")]))

/s/ ROBERT F. MANGANO

President, Chief Executive Officer and Director (Principal Executive Officer)

March 24,  2005

相关问题 更多 >