使用靓汤获取数据

2024-04-27 03:23:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用此页:http://www.afl.com.au/match-centre/2014/20/rich-v-esshttp://www.afl.com.au/match-centre/2014/19/fre-v-carl

取右边的数据。我在下面附上了一个屏幕截图。你知道吗

enter image description here

我使用了以下函数来获取数据。我已经创建了soup并将其作为参数传递给下面的函数

def fetchStats(soup):

    for i in soup.findAll("div", {"class" : "module", "id" : "season-stats"}):
        for index in i.findAll("div", {"class" : "module-content"}):
            for item in index.findAll("ul", style=False):

                li = item.findAll("li", {"class" : "major"})
                 print item.getText()
            break
        break

但它不是我想要的。我需要一个团队的所有参数都存储在一个字典中,字典的键中是团队名称,它的值有两个成员的元组-参数名称和它的值,比如

dic = {"Team 1 Name": [("Disposlas",311), ("Kicks", 190) .....], "Team 2 Name" : [("Disposlas",315), ("Kicks", 224) .....]}

请帮帮我。你知道吗


Tags: 函数indivcomforwwwmatchitem
1条回答
网友
1楼 · 发布于 2024-04-27 03:23:42

我们的想法是获取所有主队数据、客队数据和数据名称,并将它们放在单独的列表中,然后^{}它们:

from pprint import pprint
from urllib2 import urlopen
from bs4 import BeautifulSoup


url = "http://www.afl.com.au/match-centre/2014/20/rich-v-esshttp://www.afl.com.au/match-centre/2014/19/fre-v-carl"
soup = BeautifulSoup(urlopen(url))

# get team names
home_team_name = soup.find('div', class_='home-team').p.a.text.strip()
away_team_name = soup.find('div', class_='away-team').p.a.text.strip()

# get stats
season_stats = soup.find('div', id='season-stats')
home_stats = [li.text for li in season_stats.select('ul#home-team-stats > li')]
away_stats = [li.text for li in season_stats.select('ul#away-team-stats > li')]
params = [li.text for li in season_stats.select('ul.headers > li')]

stats = {home_team_name: zip(params, home_stats),
         away_team_name: zip(params, away_stats)}

pprint(stats)

印刷品:

{u'Carlton': [(u'Disposals', u'315'),
              (u'Kicks', u'224'),
              (u'Handballs', u'91'),
              (u'Free Kicks', u'22'),
              (u'Clearances', u'39'),
              (u'Centre', u'13'),
              (u'Stoppages', u'26'),
              (u'Inside 50', u'40'),
              (u'Marks in 50', u'8'),
              (u'Contested Possessions', u'127'),
              (u'Tackles', u'59'),
              (u'Hit-Outs', u'23'),
              (u'Interchanges', u'111')],
 u'Fremantle': [(u'Disposals', u'311'),
                (u'Kicks', u'190'),
                (u'Handballs', u'121'),
                (u'Free Kicks', u'14'),
                (u'Clearances', u'38'),
                (u'Centre', u'10'),
                (u'Stoppages', u'28'),
                (u'Inside 50', u'47'),
                (u'Marks in 50', u'13'),
                (u'Contested Possessions', u'124'),
                (u'Tackles', u'63'),
                (u'Hit-Outs', u'75'),
                (u'Interchanges', u'96')]}

仅供参考,您可以在这里阅读有关这些select()调用的更多信息:^{}。你知道吗

相关问题 更多 >