使用Beautifulsoup提取变量的变量列表

时间:2019-07-24 20:33:12

标签: python beautifulsoup

我有以下Resultset

<p>Cake</p><a>Cream</a><p>Coffee</p>

我也有字典:

dic[0]='p'
dic[1]='a'
dic[2]='p'

如何使用列表提取结果集的值?理想情况下,我想使用

获取字典
dic[0]='Cake'
dic[1]='Cream'
dic[2]='Coffee'

基本上,我想连续搜索Resultset列表中的下一个标签。我可以使用find_all,但这意味着我必须手动进行映射。

1 个答案:

答案 0 :(得分:1)

data = '''<p>Cake</p><a>Cream</a><p>Coffee</p>'''

dic = ['p', 'a', 'p']

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

i = iter(dic)

out = {i: tag.text for i, tag in enumerate(soup.find_all(lambda t: t.name == next(i)))}

print(out)

打印:

{0: 'Cake', 1: 'Cream', 2: 'Coffee'}

编辑:如果ResultSet中的标签不在搜索列表中,则为版本:

data = '''<span>Don't search this</span>
            <p>Cake</p>
          <span>Don't search this</span>
            <a>Cream</a>
            <p>Coffee</p>'''

lst = ['p', 'a', 'p']

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

def search(lst):
    lst = lst[:]
    tag = yield
    while lst:
        if lst[0] == tag.name:
            lst.pop(0)
            tag = yield True
            continue
        tag = yield False

it = search(lst)
next(it)
out = {i: tag.text for i, tag in enumerate(soup.find_all(lambda t: it.send(t)))}

print(out)

打印:

{0: 'Cake', 1: 'Cream', 2: 'Coffee'}

编辑2:使用CSS选择器:

data = '''<span>Don't search this</span>
            <p>Cake</p>
          <span>Don't search this</span>
            <a>Cream</a>
            <p>Coffee</p>'''

lst = ['p', 'a', 'p']

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print({i: tag.text for i, tag in enumerate(soup.select(','.join(lst)))})

打印:

{0: 'Cake', 1: 'Cream', 2: 'Coffee'}