在预标记Python之间解析

2024-09-29 23:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Python使用以下代码在PRE标记之间进行解析

s = br.open(base_url+str(string))
u = br.geturl()
seq = br.open(u)
blat = BeautifulSoup(seq)    
for res in blat.find('pre').findChildren():
        seq = res.string
        print seq

来自以下HTML源代码:

^{pr2}$

当我想解析最后一个元素时,它给出了第一个PRE-tag元素。如果有什么建议,我将不胜感激。 我希望输出如下:

00000001 taaaagatgaagtttctatcatccaaaaaatgggctacagaaacc 00000045
<<<<<<<< |||||||||||||||||||||||||||  |||||||||||||||| <<<<<<<<
41256227 taaaagatgaagtttctatcatccaaagtatgggctacagaaacc 41256183

而我现在的输出是

T
AAAAGATGA
AGTTTCTATC
ATCCAAA
A
TGGGCTACAG
AAAC
C

Tags: 代码标记brurl元素basestringres
1条回答
网友
1楼 · 发布于 2024-09-29 23:19:08

您可以使用^{}获得最后的结果:

from bs4 import BeautifulSoup

soup = BeautifulSoup(open('../index.html'), 'html5lib')

pre = soup.find_all('pre')[-1]
print pre.text.strip()

其中index.html包含您提供的html。在

它打印:

^{pr2}$

另一个选择是依赖前面的h4标记来获得适当的pre

h4 = soup.select('h4 > a[name="ali"]')[0].parent
print h4.find_next_sibling('pre').text.strip()

相关问题 更多 >

    热门问题