如何使用python的美味汤获得团队文本和分数?

时间:2016-06-12 20:06:54

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

我尝试使用此网址http://www.gosugamers.net/counterstrike/teams/7397-natus-vincere/matches获取所有团队与团队信息以及隐藏在展示按钮下的分数。我试图获得对手1和对手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)

2 个答案:

答案 0 :(得分:2)

游戏结果在隐藏的范围内(好吧,BeautifulSoup没有“隐藏”,它不是浏览器)。主分数位于span hscore级,距离span ascore级。团队名称位于具有spanspan类的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

答案 1 :(得分:-1)

查看该页面的来源,您将看到所需的所有信息都在一个包含simple gamelist profilelist类的表中。

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

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