Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Beautiful4过滤多类 从bs4导入美化组 html=”“” """ def查找(aclass): 打印(aclass) 返回aclass!=“bb” soup=BeautifulSoup(html,“lxml”) div=soup.find_all('div',attrs={'class':find}) 印刷(部门)_Python_Beautifulsoup - Fatal编程技术网

Python 如何使用Beautiful4过滤多类 从bs4导入美化组 html=”“” """ def查找(aclass): 打印(aclass) 返回aclass!=“bb” soup=BeautifulSoup(html,“lxml”) div=soup.find_all('div',attrs={'class':find}) 印刷(部门)

Python 如何使用Beautiful4过滤多类 从bs4导入美化组 html=”“” """ def查找(aclass): 打印(aclass) 返回aclass!=“bb” soup=BeautifulSoup(html,“lxml”) div=soup.find_all('div',attrs={'class':find}) 印刷(部门),python,beautifulsoup,Python,Beautifulsoup,我只想要class='aa',而不是'aa bb'或任何其他。 请帮帮我! 谢谢 这里有一个答案 这将只为您提供带有“aa”类的标记 from bs4 import BeautifulSoup html = """ <div class="aa bb"></div> <div class="aa ccc"></div> <div class="aa"></div> """ def find(a

我只想要class='aa',而不是'aa bb'或任何其他。 请帮帮我! 谢谢

这里有一个答案

这将只为您提供带有“aa”类的标记

from bs4 import BeautifulSoup

html = """
    <div class="aa bb"></div>
    <div class="aa ccc"></div>
    <div class="aa"></div>
"""


def find(aclass):
    print(aclass)
    return aclass != "bb"

soup = BeautifulSoup(html, 'lxml')

div = soup.find_all('div', attrs={'class': find})

print(div)
您还可以使用简单的:

演示:

>>来自bs4导入组
>>> 
>>>html=”“”
...     
...     
...     
... """
>>>soup=BeautifulSoup(html,“lxml”)
>>> 
>>>对于汤中的榆树。选择(“div[class=aa]”:
...     印刷品(str(elm))
... 

这一个也可以解决我的问题,但我不能投票给你的答案…对此感到抱歉…谢谢。
div = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['aa'])
soup.select("div[class=aa]")
>>> from bs4 import BeautifulSoup
>>> 
>>> html = """
...     <div class="aa bb"></div>
...     <div class="aa ccc"></div>
...     <div class="aa"></div>
... """
>>> soup = BeautifulSoup(html, 'lxml')
>>> 
>>> for elm in soup.select("div[class=aa]"):
...     print(str(elm))
... 
<div class="aa"></div>