打印 table 不带 HTML 标签以用于计算

Print table without HTML tags for use in calculations

我正在尝试将参与者的姓名和赔率从该网站获取到一个列表或 table 中,然后我可以用它进行计算。我想在如下所示的有序输出中查看数据

1.name / odds vs name / odds
2.name / odds vs name / odds

目前我似乎只能单独抓取它们,当我打印它们时它们仍然包含 HTML 标签。

import requests
from bs4 import BeautifulSoup

url = requests.get('http://www.oddschecker.com/tennis/match-coupon')
html = url.content
soup = BeautifulSoup(html)

odds = soup.find_all("span", "odds")
print(odds)

一旦我可以按上述格式构建数据,我希望能够对数据进行 运行 计算。

这个想法是遍历 table 行(tr 带有 data-market-id 属性的标签),找到所有参与者和赔率 by class names. And you need to get element's texts via get_text():

工作示例:

import requests
from bs4 import BeautifulSoup

url = requests.get('http://www.oddschecker.com/tennis/match-coupon')
html = url.content
soup = BeautifulSoup(html)

for row in soup.find_all("tr", {"data-market-id": True}):
    participants = [item.get_text(strip=True) for item in row.find_all('span', class_='fixtures-bet-name')]
    odds = [item.get_text(strip=True) for item in row.find_all('span', class_='odds')]

    print participants[0], odds[0], participants[1], odds[1]

打印:

Tomas Berdych (9/4) Roger Federer (5/12)
Milos Raonic (15/8) Rafael Nadal (8/15)
...
Vera Lapko (5/6) Julia Terziyska (5/6)
Aminat Kushkhova (11/10) Katie Swan (4/6)