【问题标题】:How to get next page outcomes for only the pages that have it?如何仅获得具有它的页面的下一页结果?
【发布时间】: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


【解决方案1】:

在将游戏分数拉出表格的for 循环之后,您可以获取分页链接。

使用此代码,您可以通过查找当前选定的页面来获取下一页。如果没有超出当前选择的页面,它(当前)将打印“找不到页面”。

paginate = soup.find('div', {'class':'paginator'})

page = paginate.find('a', {'class':'selected'})

next_page = page.find_next_sibling()
if next_page:
    print(next_page.get('href'))
else:
    print("no page found")

编辑

回应评论;这就是我想使用这段代码的方式。然后它将被添加,您可以继续循环。

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))
        # get the next page if there is one here
       page = paginate.find('a', {'class':'selected'})
       if page:
           next_page = page.find_next_sibling()
           if next_page:
               print(next_page.get('href'))
               # just append this to a list or add it to whatever you use to 
               # track the next url to crawl
               next_url = next_page.get('href')

【讨论】:

  • 好的,那么我要在 if 语句中添加我的其他函数吗?如果页面没有下一页怎么办我如何运行我的其余代码使其不会崩溃并获取我需要的信息?
  • 我想你可以将它添加到你在这里发布的代码中,但我不确定你的 match_history_url 函数是什么样子的?
  • 它只是循环每个团队的 url,它包含的示例链接就像我发布的示例链接。它包含所有团队比赛的网址页面
  • @DJRodrigue 所以我假设它返回一个列表?我编辑了我的答案,你可以像这样附加下一页。
  • 它给了我这个错误,page = paginate.find('a', {'class': 'selected'}) AttributeError: 'NoneType' object has no attribute 'find' 因为那个团队没有它试图寻找的那些属性。这就是我想要解决的问题。因为有些团队确实有下一页,有些则没有,我相信没有下一页的团队会崩溃..
猜你喜欢
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2013-11-20
  • 2016-08-23
  • 2014-02-08
  • 2012-02-23
  • 2012-02-21
  • 2019-05-25
相关资源
最近更新 更多