Python和Beautiful Soup - 搜索标签a,返回标签b,直到找到标签A.

时间:2011-09-06 14:51:19

标签: python beautifulsoup

我有两个变量,一个是“最后一卷”,另一个是“最后一期”。

我正在处理的HTML包含所有卷和问题的列表,最近是最新的。

我需要返回所有卷的href链接,这些卷和问题比我的文件更新。

所以使用下面的例子,假设我的最后一卷是13,最后一期是1,我需要返回第13卷,第2卷和第14卷的href。

由于音量很高,我现在很难接受这个......

这是我到目前为止所做的:

HTML:

<ul class="bobby">
<li><strong>Volume 14</strong></li>
<li class="">
<a href="/content/ben/cchts/2011/00000014/00000001" title="Issue 1, September 2011">Issue 1, September 2011</a>          
</li>
<li><strong>Volume 13</strong></li> 
<li class="">
<a href="/content/ben/cchts/2010/00000013/00000002" title="Issue 2, December 2010">Issue 2, December 2010</a>
</li>
<li class="">
<a href="/content/ben/cchts/2011/00000014/00000001" title="Issue 1, November 2011">Issue 1, November 2011</a>
</li>
</ul>

脚本剪辑:

results = soup.find('ul', attrs={'class' : 'bobby'})

#temp until I get it reading from file
lastVol = '13'
#find the last volume
findlastVol = results.findNext('strong', text= re.compile('Volume ' + lastVol))

#temp until I get it reading from file
lastIss = '2'
#find the last issue
findlastIss = findlastVol.findNext('a', text= re.compile('Issue ' + lastIss))

所以我可以找到最后一卷的标签并在文件上发布,但是我在第一期中经历了几次失败尝试并且停止了...

或者从顶部开始并向下移动直到满足该量和发行条件......

有人可以给我一些帮助吗?感谢。

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找findPrevious,您可以这样使用:

import BeautifulSoup
import re

content='''
<ul class="bobby">
<li><strong>Volume 14</strong></li>
<li class="">
<a href="/content/ben/cchts/2011/00000014/00000001" title="Issue 1, September 2011">Issue 1, September 2011</a>          
</li>
<li><strong>Volume 13</strong></li> 
<li class="">
<a href="/content/ben/cchts/2010/00000013/00000002" title="Issue 2, December 2010">Issue 2, December 2010</a>
</li>
<li class="">
<a href="/content/ben/cchts/2011/00000014/00000001" title="Issue 1, November 2011">Issue 1, November 2011</a>
</li>
</ul>
'''

last_volume=13
last_issue=1

soup=BeautifulSoup.BeautifulSoup(content)
results = soup.find('ul', attrs={'class' : 'bobby'})
for a_string in results.findAll('a', text=re.compile('Issue')):
    volume=a_string.findPrevious(text=re.compile('Volume'))
    volume=int(re.search(r'(\d+)',volume).group(1))
    issue=int(re.search(r'(\d+)',a_string).group(1))
    href=a_string.parent['href']
    if (volume>last_volume) or (volume>=last_volume and issue>last_issue):    
        print(volume,issue,href)

产量

(14, 1, u'/content/ben/cchts/2011/00000014/00000001')
(13, 2, u'/content/ben/cchts/2010/00000013/00000002')

答案 1 :(得分:0)

from BeautifulSoup import BeautifulSoup
content = '''<ul class="bobby">
<li><strong>Volume 14</strong></li>
<li class="">
<a href="/content/ben/cchts/2011/00000014/00000001" title="Issue 1, September     2011">Issue 1, September 2011</a>          
</li>
<li><strong>Volume 13</strong></li> 
<li class="">
<a href="/content/ben/cchts/2010/00000013/00000002" title="Issue 2, December 2010">Issue 2, December 2010</a>
</li>
<li class="">
<a href="/content/ben/cchts/2011/00000014/00000001" title="Issue 1, November 2011">Issue 1, November 2011</a>
</li>
</ul>
'''
soup = BeautifulSoup(content)
soup.prettify()
last_vol = 13
last_issue = 1

res = soup.find('ul',{"class":"bobby"})
lis = res.findAll('li')
for j in lis:
    if(j.find('strong') != None):
        vol = int(j.contents[0].string[7:])
    elif(vol > last_vol) or (vol == last_vol and int(j.contents[1]['href'][33:]) > last_issue): 
        print "Volume\t:%d" % vol
        print j.contents[1].string
        print "href\t:%s" % j.contents[1]['href']

给出

Volume  :14  
Issue 1, September 2011  
href    :/content/ben/cchts/2011/00000014/00000001  
Volume  :13  
Issue 2, December 2010  
href    :/content/ben/cchts/2010/00000013/00000002