python - 如何使用 Beautiful soup 和 python 获取团队文本和得分?

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

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

最佳答案

游戏结果位于隐藏范围内(好吧,BeautifulSoup 没有“隐藏”,它不是浏览器)。主场得分位于带有 hscore 类的 span 中,客场得分位于带有 ascore 类的 span 中。团队名称位于带有 opp1opp2 类的 span 元素内的内部 span 元素下。实现:

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

关于python - 如何使用 Beautiful soup 和 python 获取团队文本和得分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37778589/

相关文章:

python - XPath 不适用于屏幕抓取

php - 使用 PHP 或 Python 进行 Cron 作业

python - 检查一个线段是否与一组线段相交

python - python 可以同时使用相对导入和绝对导入吗?

javascript - 如何使用 cheerio 从网页中的换行符中抓取内容

python - <for> 循环的切片函数

python - 跨多进程共享基于异步等待协程的复杂对象

android - 在对 Python 的 Android 后端调用中验证 Id token

python-3.x - 结束 ='' 语法错误 : invalid syntax

python - Django - 如何防止访问其他用户的对象?