Xin chào, vì vậy tôi áp dụng find_all trên beautifulsoup object
và tìm một thứ gì đó, là bs4.element.ResultSet object
hoặc list
.beautifulsoup: find_all trên bs4.element.ResultSet đối tượng hoặc danh sách?
Tôi muốn tìm thêm trong đó, nhưng không được phép trên bs4.element.ResultSet object
. Tôi có thể lặp qua từng phần tử của bs4.element.ResultSet object
để thực hiện find_all. Nhưng tôi có thể tránh lặp lại và chỉ chuyển đổi nó trở về beautifulsoup object
?
Xem mã để biết chi tiết. Cảm ơn
html_1 = """
<table>
<thead>
<tr class="myClass">
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
</table>
"""
soup = BeautifulSoup(html_1, 'html.parser')
type(soup) #bs4.BeautifulSoup
# do find_all on beautifulsoup object
th_all = soup.find_all('th')
# the result is of type bs4.element.ResultSet or similarly list
type(th_all) #bs4.element.ResultSet
type(th_all[0:1]) #list
# now I want to further do find_all
th_all.find_all(text='A') #not work
# can I avoid this need of loop?
for th in th_all:
th.find_all(text='A') #works
Sau khi sao chép kết quả của soup.find_all để th_all, sẽ làm thay đổi th_all phản ánh trong Súp? –
Có nó sẽ. Phụ thuộc vào chức năng bạn sử dụng. Tham khảo: https://beautiful-soup-4.readthedocs.io/en/latest/#modifying-the-tree –