如何查找和存储paritcular属性及其值?

时间:2018-02-10 03:46:23

标签: python beautifulsoup

让我们考虑一下

<li>this is li</li>
<li class="c1" data="this is data">ineinieni </li>
<li class="c1" >ineinieni </li>
<li data="this is the data1">ineinieni </li>
<li data="this is the data2">ineinieni </li>

这里我只想获得数据属性的值,如下所示

this is data
this is data1
this is data2

所以我尝试使用<li[^>]*?data\s*=\s*"\s*([^>]*?)\s*"[^>]*?>re.findall进行模式匹配,但这会引发错误。

有没有办法找到使用美丽的汤获取值,我尝试了以下一个,但抛出错误,如'keyerror:data'

for j in contSoup.find_all("li"):
    print j['data']            

3 个答案:

答案 0 :(得分:1)

您可以使用__getitem__对象的bs4方法访问数据:

import re
from bs4 import BeautifulSoup as soup
s = """
<li>this is li</li>
<li class="c1" data="this is data">ineinieni </li>
<li class="c1" >ineinieni </li>
<li data="this is the data1">ineinieni </li>
<li data="this is the data2">ineinieni </li>
"""
s = soup(s, 'lxml')
final_data = [re.sub('the\s', '', i['data']) for i in s.find_all('li') if re.findall('data\=', str(i))]

输出:

['this is data', 'this is data1', 'this is data2']

答案 1 :(得分:1)

您可以尝试这种简单的方法:

>>> html = '''<li>this is li</li>
<li class="c1" data="this is data">ineinieni </li>
<li class="c1" >ineinieni </li>
<li data="this is the data1">ineinieni </li>
<li data="this is the data2">ineinieni </li>
'''
>>> soup = BeautifulSoup(html, 'html.parser')
>>> for li in soup.find_all('li', {'data': True}):
        print(li['data'])    

this is data
this is the data1
this is the data2

或者存储它:

>>> data = [x['data'] for x in soup.find_all('li', {'data': True})]
>>> data
['this is data', 'this is the data1', 'this is the data2']

答案 2 :(得分:1)

您收到了错误消息,因为您的某些<li>代码没有data属性,这意味着j["data"]KeyError

要避免这种情况,请使用可让您过滤掉这些内容的方法进行搜索。 select是一个好方法;它允许您使用紧凑的CSS选择器语法来查找所需的元素。要仅查找具有<li>属性的data代码,请尝试:

for li in contSoup.select("li[data]"):
    print li["data"]

data=True添加到find_all()作为Keyur显示是另一种方式。