Skip to main content
 首页 » 编程设计

python之从 python 中的特定标记中提取 URL

2025年05月04日11duanxz

全部。 我有一个巨大的 html 文件,其中包含如下标签:

<h3 class="r"> 
<a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm" class=l onmousedown="return clk(this.href,'','','','6','','0CDEQFjACOAM')"> 

我需要用 python 从这个页面中提取所有 url。

在循环中:

  1. 查找 <h3 class="r"> 的出现次数一个一个。

  2. 提取url

http://xrayoptics.by.ru/database/misc/goog2text.py我需要重新编写此脚本以提取在 google 上找到的所有链接。

我怎样才能做到这一点? 谢谢。

请您参考如下方法:

from BeautifulSoup import BeautifulSoup 
 
html = """<html> 
... 
<h3 class="r"> 
<a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm" class=l 
   onmousedown="return clk(this.href,'','','','6','','0CDEQFjACOAM')"> 
text</a> 
</h3> 
... 
<h3>Don't find me!</h3> 
<h3 class="r"><a>Don't find me!</a></h3> 
<h3 class="r"><a class="l">Don't error on missing href!</a></h3> 
... 
</html> 
""" 
soup = BeautifulSoup(html) 
 
for h3 in soup.findAll("h3", {"class": "r"}): 
  for a in h3.findAll("a", {"class": "l", "href": True}): 
    print a["href"]