【问题标题】:How to get team text and score using Beautiful soup with python?如何使用 Beautiful soup with python 获取团队文本和分数?
【发布时间】:2016-06-12 20:06:53
【问题描述】:

我正在尝试使用此 URL http://www.gosugamers.net/counterstrike/teams/7397-natus-vincere/matches 获取所有团队与团队信息以及隐藏在显示按钮下的分数。我试图获得 opp 1 与 opp 2 以及游戏的结果。这就是我迄今为止解决这个问题的结果。

def all_match_outcomes():

    for match_outcomes in all_match_history_url():
        page = requests.get(match_outcomes).content
        soup = BeautifulSoup(page, 'html.parser')

        for match_outcome in soup.select_one('div table.simple.gamelist.profilelist td'):
            opp_1 = match_outcome.select_one('a').find('span')
            print(opp_1)

【问题讨论】:

  • 请显示您目前拥有哪些代码以及哪些代码不起作用。
  • 是不是够清楚了还是应该把所有的代码都加进去?

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

游戏结果在隐藏跨度下(嗯,BeautifulSoup 没有“隐藏”,它不是浏览器)。主场得分在span 和hscore 类中,客场在span 和ascore 类中。团队名称位于span 元素内的内部span 元素下,其中包含opp1 和opp2 类。实施:

import requests
from bs4 import BeautifulSoup


match_outcomes = "http://www.gosugamers.net/counterstrike/teams/7397-natus-vincere/matches"
page = requests.get(match_outcomes).content
soup = BeautifulSoup(page, 'html.parser')

for row in soup.select('table.simple.gamelist.profilelist tr'):
    opp1 = row.find("span", class_="opp1").span.get_text()
    opp2 = row.find("span", class_="opp2")("span")[-1].get_text()

    opp1_score = row.find("span", class_="hscore").get_text()
    opp2_score = row.find("span", class_="ascore").get_text()

    print("%s %s:%s %s" % (opp1, opp1_score, opp2_score, opp2))

打印:

Virtus.Pro.CS 2:1 Natus Vincere
Dobry&Gaming; 0:2 Natus Vincere
GODSENT 0:2 Natus Vincere
HellRaisers 0:2 Natus Vincere
Flipsid3 Tactics 1:2 Natus Vincere
Natus Vincere 1:2 Dobry&Gaming;
mousesports.CS 1:0 Natus Vincere
mousesports.CS 0:1 Natus Vincere
...
Natus Vincere 2:1 Flipsid3 Tactics
Team Dignitas.CS 0:1 Natus Vincere

【讨论】:

  • 我也在尝试显示两个团队名称,有 2 个跨度标签,团队文本位于第二个跨度中。我如何获得团队名称?
  • @DJRodrigue 它已经在答案中,10 分钟前更新它以获取团队名称。希望对您有所帮助。
  • 是的,抱歉我没有刷新。谢谢!
【解决方案2】:

查看页面的源代码,您会看到您需要的所有信息都在一个类为simple gamelist profilelist 的表中。

阅读 Beautiful Soup Documentation ,尤其是 find 方法。

尝试在 html 源代码中查找模式,您将很快弄清楚如何迭代每个表数据 (<td>) 以及如何提取团队等。

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    相关资源
    最近更新 更多