如何获得美丽的汤从href和类获得链接?

时间:2012-09-26 22:46:25

标签: python html download beautifulsoup

我正在编写一个脚本从网站下载多个FLAC,我正在使用Beautiful Soup来获取flac链接并使用 urlopen下载链接

我希望BS搜索以.flac结尾的链接(我不知道文件名,只是扩展名EX:1文件是XXX.flac,另一个是YYY.flac)< / p>

flac文件的HTML在这里

<b><a class=location href="/soundtracks/index.php">Soundtracks</a><font class=location> &raquo </font><a href="/soundtracks/highquality/index.php">High Quality Game 
Soundtracks [FLAC]</a><font class=location> &raquo </font><a href="/soundtracks/highquality/Metal_Gear_20th_Anniversary/72">Metal Gear 20th Anniversary</a><font class=location> &raquo 01 Metal Gear 20 Years History -Past, Present, Future- Download</font></b><h1>Metal Gear 20th Anniversary Download Links:</h1><a style="font-size: 16px; font-weight:bold;" href="http://50.7.161.234/bks/94/245/Music/[029] MG 20th Anniversary [FLAC]/01 Metal Gear 20 Years History -Past, Present, Future-.flac">Metal Gear 20th Anniversary - 01 Metal Gear 20 Years History -Past, Present, Future-</a> <font face="Verdana" style="font-size: 16px;">Format: FLAC, Size: 76M</font><br> <font face="Verdana" style="font-size: 10px;"><b>Note: If the file starts playing in your browser window, try right-clicking and "Save Target As"</b></font><br>

我试图找到id。 t = soup.find(id="flac")但我没有得到任何相关结果。我对此很茫然,我不知道有什么方法可以解决它

如何让BS搜索并找到文件链接,然后将该文件链接分配给变量?

import mechanize
import urllib, urllib2, re
from bs4 import BeautifulSoup
####MECHANIZE####
br = mechanize.Browser()
res = br.open("http://www.emuparadise.me/soundtracks/highquality/Metal_Gear_20th_Anniversary/72")
a = 2 #COUNTER FOR LOOP
br.follow_link(text_regex='Download', nr=a)
b = br.geturl() #GETS THE URL
print b


page = urllib2.urlopen(b).read()
soup = BeautifulSoup(page)
soup.prettify()
t = soup.find(id="")
print t

1 个答案:

答案 0 :(得分:3)

您的代码正在尝试匹配链接到这些flacs的锚标记中不存在的id属性。

而是使用正则表达式来匹配href中结束的.flac

t = soup.find_all(href=re.compile(".flac$"))