7
元素我想要做這樣的事情:BeautifulSoup4:選擇屬性不等於x
soup.find_all('td', attrs!={"class":"foo"})
我想找到沒有Foo類的所有TD。
顯然以上不起作用,有什麼用?
元素我想要做這樣的事情:BeautifulSoup4:選擇屬性不等於x
soup.find_all('td', attrs!={"class":"foo"})
我想找到沒有Foo類的所有TD。
顯然以上不起作用,有什麼用?
BeautifulSoup
真正使「湯」美麗,易於使用。
您can pass a function在屬性值:
soup.find_all('td', class_=lambda x: x != 'foo')
演示:
>>> from bs4 import BeautifulSoup
>>> data = """
... <tr>
... <td>1</td>
... <td class="foo">2</td>
... <td class="bar">3</td>
... </tr>
... """
>>> soup = BeautifulSoup(data)
>>> for element in soup.find_all('td', class_=lambda x: x != 'foo'):
... print element.text
...
1
3
@alecxe是的,謝謝。 – kylex