问题描述: 在使用Beautiful Soup的find()方法时,发现无法找到所有class的结果。
解决方法:
from bs4 import BeautifulSoup
html = """
Div 1
Div 2
Div 3
"""
soup = BeautifulSoup(html, 'html.parser')
divs = soup.find_all(class_="class1")
for div in divs:
print(div.text)
输出结果为:
Div 1
Div 3
from bs4 import BeautifulSoup
html = """
Div 1
Div 2
Div 3
"""
soup = BeautifulSoup(html, 'html.parser')
divs = soup.select('.class1')
for div in divs:
print(div.text)
输出结果为:
Div 1
Div 3
以上两种方法都可以解决Beautiful Soup的find()方法无法找到所有class的结果的问题。