Kam*_*ish 3 python if-statement beautifulsoup
有没有办法让 BeautifulSoup 寻找一个类,如果它存在然后运行脚本?我正在尝试这个:
if soup.find_all("div", {"class": "info"}) == True:
print("Tag Found")
Run Code Online (Sandbox Code Playgroud)
我也试过,但它没有用,并给出了一个关于属性太多的错误:
if soup.has_attr("div", {"class": "info"})
print("Tag Found")
Run Code Online (Sandbox Code Playgroud)
你非常接近......soup.findall
如果没有找到任何匹配项,它将返回一个空列表。您的控制语句正在检查其返回的文字bool
值。相反,您需要通过省略==True
if soup.find_all("div", {"class": "info"}):
print("Tag Found")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9465 次 |
最近记录: |