【发布时间】:2011-05-24 12:19:38
【问题描述】:
我的 html 包含这样的条目:
<div class="entry">
<h3 class="foo">
<a href="http://www.example.com/blog-entry-slug"
rel="bookmark">Blog Entry</a>
</h3>
...
</div>
我想提取文本“博客条目”(以及许多其他属性,所以我正在寻找一个通用的答案)。
在 jQuery 中,我会这样做
$('.entry a[rel=bookmark]').text()
我在 Python 中最接近的是:
from BeautifulSoup import BeautifulSoup
import soupselect as soup
rawsoup = BeautifulSoup(open('fname.html').read())
for entry in rawsoup.findAll('div', 'entry'):
print soup.select(entry, 'a[rel=bookmark]')[0].string.strip()
soupselect from http://code.google.com/p/soupselect/.
Soupselect 不像 jQuery 那样理解完整的 CSS3 选择器语法。 Python中有这样的野兽吗?
【问题讨论】:
标签: jquery python css-selectors beautifulsoup