Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html BeautifulSoup。上一次搜索返回无_Html_Beautifulsoup - Fatal编程技术网

Html BeautifulSoup。上一次搜索返回无

Html 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

下面是示例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 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