1

嗨,我已经使用此标签从 html 文件中查找标签的内容。

def everything_between(text,begin,end):
    idx1=content.find(begin)
    idx2=content.find(end,idx1)
    return content[idx1+len(begin):idx2].strip()

content=open('page.html').read()
title=everything_between(content,'<ul class="members">','</ul>')
interesting=everything_between(content,'INTERESTING:','bodystuff')
print(title)

但是在标签<ul class="member">中有多个<ahref>标签,我想获取它们之间的<a href>内容 <a href="/history/member/">

脚本应该获取<a href="/history/member/"></a>.

我怎样才能做到这一点?

4

1 回答 1

0

http://www.crummy.com/software/BeautifulSoup/

soup.title
# <title>The Dormouse's story</title>

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# <p class="title"><b>The Dormouse's story</b></p>

soup.p['class']
# u'title'

soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
于 2012-11-05T12:29:46.247 回答