BeautifulSoup4 Python 3.4刮痧问题

时间:2016-04-22 02:23:43

标签: python web-scraping beautifulsoup python-3.4

尝试从具有相同标记名称的一些不同信息的网站提取数据。 例如:

<td class=pizza>cheese </td>
<td class=pizza> deluxe</td>
<td class=pizza> pepperoni</td>
<td class=pizza> raccoon</td>
<td class=pizza> mushshroom</td>
<td class=pizza> anchovy</td>

如何编写一系列网页抓取(使用BeautifulSoup 4)来选择一个不是奶酪的网页。 (说我想要豪华)。不会是这样的:

pizza = soup.find("td", {"class": "pizza"})
print (pizza) #This will give me cheese as a result, and I want deluxe.

1 个答案:

答案 0 :(得分:3)

如果您不想具体cheese,可以使用a function过滤掉它:

soup.find("td", class_="pizza", text=lambda text: text and text.strip() != "cheese")

或者,如果您希望第二个td元素带有class="pizza"

soup.find_all("td", class_="pizza")[1]
相关问题