提取href目标锚文本

时间:2019-02-28 18:21:06

标签: python html web-scraping beautifulsoup

*更新:我现在获得了Href链接。只需进行搜索即可获取每个项目之间的所有文本。

这是我的代码: 1.获取开始和结束数据。

import requests
from bs4 import BeautifulSoup
import re
import urllib
new_text=urllib.request.urlopen("https://www.sec.gov/Archives/edgar/data/1294017/000119312505142547/0001193125-05-142547.txt") 
soup = BeautifulSoup(new_text, 'lxml')
results = soup.findAll("a", {"name" : True})
print(results)

所以我得到这些:

<a name="toc"></a>, <a name="toc51579_1"></a>, <a name="toc51579_2"></a>,
  1. 获取每个起点和终点之间的文本。 (在这里,我想做一个循环以从上面的列表中获取第一项和第二项,插入到re.search中并获取它们之间的所有文本。但是我现在仍然停留在这一点。我无法使该循环正常工作。我认为在将第一个和第二个数据点插入到re.search函数中作为文本时,我犯了一个错误。

    对于我的枚举(结果): new_text = re.search(r''+ re.escape(results [i])+ re.escape('。*?')+ re.escape(results(i + 1)),汤,re.DOTALL)。组() 打印(新文本)

原始问题:

假设我可以获取Anchor Href的链接,那么如何在文本中锚定Href的位置之间提取文本?

所以基本上,我有

<A HREF="#toc51579_1">Summary</A>

<A HREF="#toc51579_2">Risk Factors</A>

我想按照锚href转到“摘要”页面,将所有文本拉到“风险因素”页面。

例如:从...开始

<A NAME="toc51579_1"></A>Summary </B></FONT></P>

至       危险因素

我的第一篇文章,所以请耐心等待。 :)

非常感谢您。

这是目录页面。我不需要这里的文字。这是为了显示锚点Hrefs所在的位置。

    <TR>
<TD WIDTH="88%"></TD>
<TD VALIGN="bottom" WIDTH="8%"></TD>
<TD></TD></TR>
<TR>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;</FONT></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="bottom" ALIGN="center" STYLE="border-bottom:1px solid #000000"><FONT STYLE="font-family:Times New Roman" SIZE="1"><B>Page</B></FONT></TD></TR>
<TR>
<TD VALIGN="top"> <P STYLE="margin-left:1.00em; text-indent:-1.00em"><FONT STYLE="font-family:Times New Roman" SIZE="2"><A HREF="#toc51579_1">Summary</A></FONT></P></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="bottom" ALIGN="right"><FONT STYLE="font-family:Times New Roman" SIZE="2">1</FONT></TD></TR>
<TR>
<TD VALIGN="top"> <P STYLE="margin-left:1.00em; text-indent:-1.00em"><FONT STYLE="font-family:Times New Roman" SIZE="2"><A HREF="#toc51579_2">Risk Factors</A></FONT></P></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="bottom" ALIGN="right"><FONT STYLE="font-family:Times New Roman" SIZE="2">15</FONT></TD></TR>

1 个答案:

答案 0 :(得分:1)

您想要文本,而不是实际的href值正确吗?文本值在<a>标记内。 .find_all('a')也是如此。然后遍历这些元素,并使用.text

来获取文本
html = '''    <TR>
<TD WIDTH="88%"></TD>
<TD VALIGN="bottom" WIDTH="8%"></TD>
<TD></TD></TR>
<TR>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;</FONT></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="bottom" ALIGN="center" STYLE="border-bottom:1px solid #000000"><FONT STYLE="font-family:Times New Roman" SIZE="1"><B>Page</B></FONT></TD></TR>
<TR>
<TD VALIGN="top"> <P STYLE="margin-left:1.00em; text-indent:-1.00em"><FONT STYLE="font-family:Times New Roman" SIZE="2"><A HREF="#toc51579_1">Summary</A></FONT></P></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="bottom" ALIGN="right"><FONT STYLE="font-family:Times New Roman" SIZE="2">1</FONT></TD></TR>
<TR>
<TD VALIGN="top"> <P STYLE="margin-left:1.00em; text-indent:-1.00em"><FONT STYLE="font-family:Times New Roman" SIZE="2"><A HREF="#toc51579_2">Risk Factors</A></FONT></P></TD>
<TD VALIGN="bottom"><FONT SIZE="1">&nbsp;&nbsp;</FONT></TD>
<TD VALIGN="bottom" ALIGN="right"><FONT STYLE="font-family:Times New Roman" SIZE="2">15</FONT></TD></TR>'''

import bs4

soup = bs4.BeautifulSoup(html, 'html.parser')

alpha = soup.find_all('a')

for ele in alpha:
    print (ele.text)

输出:

Summary
Risk Factors

如果碰巧还有其他没有<a>的{​​{1}}标签,但是您只想要带有href的标签,只需将其添加到{{1} }

href