Python 属性中的Beautifulsoup搜索关键字
在得到源代码后,我Python 属性中的Beautifulsoup搜索关键字,python,beautifulsoup,Python,Beautifulsoup,在得到源代码后,我 [<div amy="sister" tommy="brother" julie="link1">E11</div>] [<div amy="sister" tommy="brother" julie="link2_cat">E12</div>] [<div amy="sister" tommy="brother" julie="link3_cat">E13</div>] 但不起作用导入bs4 impo
[<div amy="sister" tommy="brother" julie="link1">E11</div>]
[<div amy="sister" tommy="brother" julie="link2_cat">E12</div>]
[<div amy="sister" tommy="brother" julie="link3_cat">E13</div>]
但不起作用导入bs4
import bs4
html = '''<div amy="sister" tommy="brother" julie="link1">E11</div>
<div amy="sister" tommy="brother" julie="link2_cat">E12</div>
<div amy="sister" tommy="brother" julie="link3_cat">E13</div>'''
soup = bs4.BeautifulSoup(html, 'lxml')
soup.find_all('div',{"julie":re.compile("_cat")})
html=''E11
E12
E13''
soup=bs4.BeautifulSoup(html,“lxml”)
soup.find_all('div',{“julie”:re.compile('u cat”)})
输出:
[E12,
E13]
您应该在soup
对象中使用find_all()
,而不是在标记列表中使用。导入bs4
html=''E11
E12
E13''
soup=bs4.BeautifulSoup(html,“lxml”)
soup.find_all('div',{“julie”:re.compile('u cat”)})
输出:
[E12,
E13]
您应该在
soup
对象中使用find_all()
,而不是在标记列表中使用。如果您想获得julie
标记属性值,请:
还有一种更简洁的方法来匹配所需的元素-:
$=
选择器表示“以结束”。如果您想获得julie
标记属性值:
还有一种更简洁的方法来匹配所需的元素-:
$=
选择器表示“以结束”
import bs4
html = '''<div amy="sister" tommy="brother" julie="link1">E11</div>
<div amy="sister" tommy="brother" julie="link2_cat">E12</div>
<div amy="sister" tommy="brother" julie="link3_cat">E13</div>'''
soup = bs4.BeautifulSoup(html, 'lxml')
soup.find_all('div',{"julie":re.compile("_cat")})
[<div amy="sister" julie="link2_cat" tommy="brother">E12</div>,
<div amy="sister" julie="link3_cat" tommy="brother">E13</div>]
In [5]: [tag["julie"] for tag in soup.find_all('div',{"julie":re.compile("_cat")})]
Out[5]: ['link2_cat', 'link3_cat']
In [6]: [tag["julie"] for tag in soup.select('div[julie$=_cat]')]
Out[6]: ['link2_cat', 'link3_cat']