【问题标题】:problem scraping with BeautifulSoup使用 BeautifulSoup 抓取问题
【发布时间】:2011-07-03 13:46:11
【问题描述】:

我正在尝试使用 BeautifulSoup 抓取 url http://www.kat.ph/search/beatles/?categories[]=music

torrents = bs.findAll('tr',id = re.compile('torrent_*'))

torrents 获取该页面上的所有种子,现在种子的每个元素都包含一个 tr 元素。

我的问题是 len(torrents[0].td) 为 5,但我无法遍历 td。我的意思是像 for x in torrents[o].td 这样的东西不起作用。

我为 torrent[0] 获取的数据是:

<tr class="odd" id="torrent_2962816">
<td class="fontSize12px torrentnameCell">
<div class="iaconbox floatedRight">
<a title="Torrent magnet link" href="magnet:?xt=urn:btih:0898a4b562c1098eb69b9b801c61a51d788df0f5&amp;dn=the+beatles+2009+greatest+hits+cdrip+ikmn+reupld&amp;tr=http%3A%2F%2Ftracker.publicbt.com%2Fannounce" onclick="_gaq.push(['_trackEvent', 'Download', 'Magnet Link', 'Music']);" class="imagnet icon16"></a>
<a title="Download torrent file" href="http://torrage.com/torrent/0898A4B562C1098EB69B9B801C61A51D788DF0F5.torrent?title=[kat.ph]the.beatles.2009.greatest.hits.cdrip.ikmn.reupld" onclick="_gaq.push(['_trackEvent', 'Download', 'Download torrent file', 'Music']);" class="idownload icon16"></a>
<a class="iPartner2 icon16" href="http://www.downloadweb.org/checking.php?acode=b146a357c57fddd450f6b5c446108672&amp;r=d&amp;qb=VGhlIEJlYXRsZXMgWzIwMDldIEdyZWF0ZXN0IEhpdHMgQ0RSaXAtIGlLTU4gUmVVUGxk" onclick="_gaq.push(['_trackEvent', 'Download', 'Download movie']);"></a>
<a class="iverif icon16" href="/the-beatles-2009-greatest-hits-cdrip-ikmn-reupld-t2962816.html" title="Verified Torrent"></a> <a rel="2962816,0" class="icomment" href="/the-beatles-2009-greatest-hits-cdrip-ikmn-reupld-t2962816.html#comments_tab">
<span class="icommentdiv"></span>145
    </a>
</div>
<div class="torrentname">
<a href="/the-beatles-2009-greatest-hits-cdrip-ikmn-reupld-t2962816.html" class="torType musicType"></a>
<a href="/the-beatles-2009-greatest-hits-cdrip-ikmn-reupld-t2962816.html">The <strong class="red">Beatles</strong> [2009] Greatest Hits CDRip- iKMN ReUPld</a>
<span>
                Posted by <a class="plain" href="/user/iKMN/">iKMN</a>
<img src="http://static.kat.ph/images/verifup.png" alt="verified" /> in 
                    <span id="cat_2962816">
<a href="/music/">Music</a>
</span></span>
</div>
</td>
<td class="nobr">168.26 <span>MB</span></td>
<td>42</td>
<td>1&nbsp;year</td>
<td class="green">1368</td>
<td class="red lasttd">94</td>
</tr>

【问题讨论】:

  • 你为 RIAA 工作吗?
  • 你为什么要 len(torrents[0].td 然后迭代完全不同的东西???
  • @Matt - 这仍然是一个完全有效的编程问题,没有说明任何恶意意图。 @Bunny,您介意编辑这篇文章以包含数据样本吗?如果该链接中断,此问题的整个上下文也会中断
  • @Tim 我并不是在暗示它不是,只是在开玩笑。事实上,既然我已经看到了这个问题,我将开始研究 BeautifulSoup :)
  • 你能发布一个你想要的输出的例子吗?

标签: python beautifulsoup web-scripting


【解决方案1】:

我建议使用 lxml 或代替 BeautifulSoup,以及其他可以使用 xpath 获取链接的强大功能:

import lxml.html
doc = lxml.html.parse('http://www.kat.ph/search/beatles/?categories[]=music')
links = doc.xpath('//a[contains(@class,"idownload")]/@href')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-27
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多