解析变化的标签BeautifulSoup [英] Parsing changing tags BeautifulSoup

查看:58
本文介绍了解析变化的标签BeautifulSoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的标签持续更改如下:

If my tags keep on changing as follows:

<tr id="CN13FUT">
<tr id="CU13FUT">
<tr id="CZ13FUT">
<tr id="CH14FUT">
[...]

在使用BeautifulSoup时,我该如何阅读?这是我需要的帮助:

How can I read this in using BeautifulSoup? This is what I need help with:

table = BeautifulSoup(page)
for tr in table.findAll('tr', attrs = {'id': 'something_here'))
   print tr

我不想只使用 table.findAll('tr'),因为可能还有其他我不想使用的 tr 标签如上面的格式所示.

I don't want to use just table.findAll('tr') because there might be other tr tags that I don't want, I only want as how it is shown in the format above.

推荐答案

如果所有id属性都以"FUT"结尾,则

If all id attributes are ending in "FUT", then

for tr in table.findAll(id=re.compile('FUT$')):
    print(tr)
    print(tr['id']) # to print the id attributes

如果所有id属性的长度都相同(7),则

If all id attributes are the same length (7), then

for tr in table.findAll('tr', id=lambda x: x and len(x)==7):
    print(tr['id']) # to print the id attributes

这篇关于解析变化的标签BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆