Html BeautifulSoup。上一次搜索返回无
下面是示例HTMLHtml BeautifulSoup。上一次搜索返回无,html,beautifulsoup,Html,Beautifulsoup,下面是示例HTML <tr id="gift1" class="gift"><td> Vegetable Basket </td><td> This vegetable basket is the perfect gift for your health conscious (or overweight) friends! <span class="excitingNote">Now with super-colorful bell p
<tr id="gift1" class="gift"><td>
Vegetable Basket
</td><td>
This vegetable basket is the perfect gift for your health conscious (or overweight) friends!
<span class="excitingNote">Now with super-colorful bell peppers!</span>
</td><td>
$15.00
</td><td>
<img src="../img/gifts/img1.jpg">
</td></tr>
bsObj.find(“img”,“src”:./img/gifts/img1.jpg”)。父级
返回:
<td>
<img src="../img/gifts/img1.jpg">
</td>
但是bsObj.find(“img”,“src”:./img/gives/img1.jpg”).parent.previous\u sibling
始终返回None
这不应该返回价格标签吗?首先,你有一个打字错误-
以前的兄弟姐妹
vs以前的兄弟姐妹
:
>>> from bs4 import BeautifulSoup
>>>
>>> data = """<tr id="gift1" class="gift"><td>
... Vegetable Basket
... </td><td>
... This vegetable basket is the perfect gift for your health conscious (or overweight) friends!
... <span class="excitingNote">Now with super-colorful bell peppers!</span>
... </td><td>
... $15.00
... </td><td>
... <img src="../img/gifts/img1.jpg">
... </td></tr>"""
>>>
>>> soup = BeautifulSoup(data, "html.parser")
>>>
>>> image_url = "../img/gifts/img1.jpg"
>>>
>>> image = soup.find("img", src=image_url)
>>> price = image.parent.previous_sibling.get_text(strip=True)
>>> print(price)
$15.00
哦,是打字错误。它没有返回任何结果。所以我从来没有想过拼写错误。泰
>>> from bs4 import BeautifulSoup
>>>
>>> data = """<tr id="gift1" class="gift"><td>
... Vegetable Basket
... </td><td>
... This vegetable basket is the perfect gift for your health conscious (or overweight) friends!
... <span class="excitingNote">Now with super-colorful bell peppers!</span>
... </td><td>
... $15.00
... </td><td>
... <img src="../img/gifts/img1.jpg">
... </td></tr>"""
>>>
>>> soup = BeautifulSoup(data, "html.parser")
>>>
>>> image_url = "../img/gifts/img1.jpg"
>>>
>>> image = soup.find("img", src=image_url)
>>> price = image.parent.previous_sibling.get_text(strip=True)
>>> print(price)
$15.00
>>> price = image.find_previous(text=lambda text: text and text.strip().startswith("$")).strip()
>>> print(price)
$15.00