Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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
Python 从td、span和style中提取的美味汤 23 - 1 - 13 (1.77)_Python_Beautifulsoup - Fatal编程技术网

Python 从td、span和style中提取的美味汤 23 - 1 - 13 (1.77)

Python 从td、span和style中提取的美味汤 23 - 1 - 13 (1.77),python,beautifulsoup,Python,Beautifulsoup,我在找那些号码23-1-13。如何提取它们?我在Python中使用的是BeautifulSoup。您需要在span上进行查找,而不是td,因为正是该元素包含您正在搜索的类: <td><span data-tooltip="some text" class="tooltip button is-success is-small" style="cursor: default;"> 23 - 1 - 13 (<span class="is-danger" style="

我在找那些号码23-1-13。如何提取它们?我在Python中使用的是BeautifulSoup。

您需要在
span
上进行查找,而不是
td
,因为正是该元素包含您正在搜索的类:

<td><span data-tooltip="some text" class="tooltip button is-success is-small" style="cursor: default;">
23 - 1 - 13 (<span class="is-danger" style="font-weight: 700;">1.77</span>)
您还可以使用
soup.span.text
span
标记中查找文本。然后使用一些标准Python将其拆分并将其转换为数字。如果有多个元素,可以按如下方式进行:

soup.find_all('span', class_="tooltip button is-success is-small")

你的代码是什么?你尝试了什么?我尝试了soup.find_all('td',class=“tooltip button is success is small”),但它没有显示任何内容用代码和故障描述编辑你的问题,阅读一些带有标签的其他问题
beautifulsou
它可以帮助你创建一个好问题。在只有一个元素的情况下,它可以很好地工作,但我有大约20个结果,就像在帖子中一样。我正在尝试接收所有的数字。我尝试了:results=soup.find_all('span',class=“tooltip button is success is small”)查找结果中的元素:numbers=[int(v)for v in elements.span.text.strip().split('')[:-1]如果v!='-'],但我收到了0个结果。您需要编辑您的问题以提供一个更现实的示例。我已经更新了脚本,展示了一种可能的方法。
from bs4 import BeautifulSoup

html = """
<td><span data-tooltip="some text" class="tooltip button is-success is-small" style="cursor: default;">23 - 1 - 13 (<span class="is-danger"style="font-weight: 700;">1.77</span>)</td>
<td><span data-tooltip="some text" class="tooltip button is-success is-small" style="cursor: default;">23 - 1 - 13 (<span class="is-danger"style="font-weight: 700;">1.77</span>)</td>
<td><span data-tooltip="some text" class="tooltip button is-success is-small" style="cursor: default;">23 - 1 - 13 (<span class="is-danger"style="font-weight: 700;">1.77</span>)</td>
"""

soup = BeautifulSoup(html, "html.parser")

for span in soup.find_all('span', class_='tooltip button is-success is-small'):
    numbers = [int(v) for v in span.text.strip().split(' ')[:-1] if v != '-']
    print(numbers)