【发布时间】:2016-06-21 19:42:57
【问题描述】:
所以这段代码让我得到了所有的比赛结果,a 队 vs 队和比赛的比分。例如像这样的团队http://www.gosugamers.net/counterstrike/teams/7395-mousesports-cs/matches。但是这段代码只得到第一页的结果,我试图得到每个可用页面的所有结果。问题是一些团队没有下一页按钮,所以当我尝试实现该代码时程序崩溃了。我如何编写代码来获取下一页并继续获取结果,如果团队比赛链接没有下一页就继续?
def all_match_outcomes():
for match_outcomes in match_history_url():
rest_server(True)
page = requests.get(match_outcomes).content
soup = BeautifulSoup(page, 'html.parser')
team_name_element = soup.select_one('div.teamNameHolder')
team_name = team_name_element.find('h1').text.replace('- Team Overview', '')
for match_outcome in soup.select('table.simple.gamelist.profilelist tr'):
opp1 = match_outcome.find('span', {'class': 'opp1'}).text
opp2 = match_outcome.find('span', {'class': 'opp2'}).text
opp1_score = match_outcome.find('span', {'class': 'hscore'}).text
opp2_score = match_outcome.find('span', {'class': 'ascore'}).text
if match_outcome(True): # If teams have past matches
print(team_name, '%s %s:%s %s' % (opp1, opp1_score, opp2_score, opp2))
【问题讨论】:
-
什么是无下一步按钮的示例?您是在谈论页面末尾的下一个按钮还是确切地说是什么?
-
所以在底部的链接上,它会显示页数,然后是下一页或最后一页..有些球队根本没有这个,因为他们玩的比赛不多或无论如何。因此,如果我合并了一个可以让我进入下一页的代码,它会崩溃并说该页面不包含所述标签或我用来查找下一页的任何内容。
标签: python-3.x web-scraping beautifulsoup