首页 > 解决方案 > 我想在 python 3 中以不同的方式合并两个列表

问题描述

因此,我设法从 metacritic 获得了有史以来最好的 100 场比赛的一些数据,他们的标题和 metacritic 分数,但我不知道如何像第一个分数然后标题那样合并它们。我能得到一些帮助吗?

import requests
from bs4 import BeautifulSoup

metacritic_url = 'https://www.metacritic.com/browse/games/score/metascore/all/all/filtered'

headers = {'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 10066.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}

r = requests.get(metacritic_url, headers=headers)

soup = BeautifulSoup(r.content.decode(),'html.parser')

games = soup.find_all('div',{'class':'basic_stat product_title'})

scores = soup.find_all('div',{'class':'metascore_w small game positive'})

print(len(games))
print(len(scores))

for game in games:
    game_title = game.text
    game_title = game_title.replace("   ","")
    print("**********************************")
    print(game_title)

for score in scores:
    score = score.text
    print(score)

标签: pythonlist

解决方案


import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}
r = requests.get(
    'https://www.metacritic.com/browse/games/score/metascore/all/all/filtered', headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
title = []
score = []
data = []
for item in soup.findAll('div', {'class': 'basic_stat product_title'}):
    title.append(item.text.replace("\n", "").replace("   ", ""))
for item in soup.findAll('div', {'class': 'metascore_w small game positive'}):
    score.append(item.text)

for a, b in zip(title, score):
    print("Game: {:<50} Score: {:>2}".format(a, b))

输出:

Game:  The Legend of Zelda: Ocarina of Time(N64)         Score: 99
Game:  Tony Hawk's Pro Skater 2(PS)                      Score: 98
Game:  Grand Theft Auto IV(PS3)                          Score: 98
Game:  SoulCalibur(DC)                                   Score: 98
Game:  Grand Theft Auto IV(X360)                         Score: 98
Game:  Super Mario Galaxy(WII)                           Score: 97
Game:  Super Mario Galaxy 2(WII)                         Score: 97
Game:  Red Dead Redemption 2(XONE)                       Score: 97
Game:  Grand Theft Auto V(XONE)                          Score: 97
Game:  Grand Theft Auto V(PS3)                           Score: 97
Game:  Grand Theft Auto V(X360)                          Score: 97
Game:  Tony Hawk's Pro Skater 2(DC)                      Score: 97
Game:  The Legend of Zelda: Breath of the Wild(Switch)   Score: 97
Game:  Tony Hawk's Pro Skater 3(PS2)                     Score: 97
Game:  Perfect Dark(N64)                                 Score: 97
Game:  Red Dead Redemption 2(PS4)                        Score: 97
Game:  Grand Theft Auto V(PS4)                           Score: 97
Game:  Metroid Prime(GC)                                 Score: 97
Game:  Grand Theft Auto III(PS2)                         Score: 97
Game:  Super Mario Odyssey(Switch)                       Score: 97
Game:  Halo: Combat Evolved(XBOX)                        Score: 97
Game:  NFL 2K1(DC)                                       Score: 97
Game:  Half-Life 2(PC)                                   Score: 96
Game:  Grand Theft Auto V(PC)                            Score: 96
Game:  The Legend of Zelda: Breath of the Wild(WIIU)     Score: 96
Game:  BioShock(X360)                                    Score: 96
Game:  GoldenEye 007(N64)                                Score: 96
Game:  Uncharted 2: Among Thieves(PS3)                   Score: 96
Game:  Resident Evil 4(GC)                               Score: 96
Game:  The Orange Box(X360)                              Score: 96
Game:  The Orange Box(PC)                                Score: 96
Game:  Batman: Arkham City(PS3)                          Score: 96
Game:  Tekken 3(PS)                                      Score: 96
Game:  Mass Effect 2(X360)                               Score: 96
Game:  The Legend of Zelda: Twilight Princess(GC)        Score: 96
Game:  The Elder Scrolls V: Skyrim(X360)                 Score: 96
Game:  Half-Life(PC)                                     Score: 96
Game:  Resident Evil 4(PS2)                              Score: 96
Game:  The Legend of Zelda: The Wind Waker(GC)           Score: 96
Game:  Gran Turismo(PS)                                  Score: 96
Game:  BioShock(PC)                                      Score: 96
Game:  Metal Gear Solid 2: Sons of Liberty(PS2)          Score: 96
Game:  Grand Theft Auto Double Pack(XBOX)                Score: 96
Game:  Baldur's Gate II: Shadows of Amn(PC)              Score: 95
Game:  Grand Theft Auto: San Andreas(PS2)                Score: 95
Game:  Grand Theft Auto: Vice City(PS2)                  Score: 95
Game:  LittleBigPlanet(PS3)                              Score: 95
Game:  The Legend of Zelda Collector's Edition(GC)       Score: 95
Game:  Red Dead Redemption(PS3)                          Score: 95
Game:  Gran Turismo 3: A-Spec(PS2)                       Score: 95
Game:  Halo 2(XBOX)                                      Score: 95
Game:  The Legend of Zelda: A Link to the Past(GBA)      Score: 95
Game:  The Legend of Zelda: Majora's Mask(N64)           Score: 95
Game:  The Last of Us(PS3)                               Score: 95
Game:  The Legend of Zelda: Twilight Princess(WII)       Score: 95
Game:  Madden NFL 2003(PS2)                              Score: 95
Game:  Tony Hawk's Pro Skater 2(GBA)                     Score: 95
Game:  The Last of Us Remastered(PS4)                    Score: 95
Game:  Portal 2(PC)                                      Score: 95
Game:  Red Dead Redemption(X360)                         Score: 95
Game:  Portal 2(X360)                                    Score: 95
Game:  Metal Gear Solid V: The Phantom Pain(XONE)        Score: 95
Game:  Portal 2(PS3)                                     Score: 95
Game:  World of Goo(WII)                                 Score: 94
Game:  BioShock Infinite(PS3)                            Score: 94
Game:  Final Fantasy IX(PS)                              Score: 94
Game:  Call of Duty: Modern Warfare 2(PS3)               Score: 94
Game:  God of War(PS4)                                   Score: 94
Game:  Tony Hawk's Pro Skater 4(PS2)                     Score: 94
Game:  Devil May Cry(PS2)                                Score: 94
Game:  Call of Duty 4: Modern Warfare(PS3)               Score: 94
Game:  Madden NFL 2002(PS2)                              Score: 94
Game:  Batman: Arkham City(X360)                         Score: 94
Game:  The Elder Scrolls V: Skyrim(PC)                   Score: 94
Game:  Mass Effect 2(PC)                                 Score: 94
Game:  The Legend of Zelda: Ocarina of Time 3D(3DS)      Score: 94
Game:  Chrono Cross(PS)                                  Score: 94
Game:  Celeste(XONE)                                     Score: 94
Game:  BioShock(PS3)                                     Score: 94
Game:  Mass Effect 2(PS3)                                Score: 94
Game:  Grand Theft Auto: Vice City(PC)                   Score: 94
Game:  Madden NFL 2004(PS2)                              Score: 94
Game:  Gears of War(X360)                                Score: 94
Game:  The Elder Scrolls IV: Oblivion(X360)              Score: 94
Game:  Sid Meier's Civilization II(PC)                   Score: 94
Game:  Quake(PC)                                         Score: 94
Game:  Call of Duty 4: Modern Warfare(X360)              Score: 94
Game:  BioShock Infinite(PC)                             Score: 94
Game:  Halo 3(X360)                                      Score: 94
Game:  Ninja Gaiden Black(XBOX)                          Score: 94
Game:  The Elder Scrolls IV: Oblivion(PC)                Score: 94
Game:  Street Fighter IV(PS3)                            Score: 94
Game:  Super Mario Advance 4: Super Mario Bros. 3(GBA)   Score: 94
Game:  Jet Grind Radio(DC)                               Score: 94
Game:  Metal Gear Solid(PS)                              Score: 94
Game:  Grim Fandango(PC)                                 Score: 94
Game:  Tom Clancy's Splinter Cell Chaos Theory(XBOX)     Score: 94
Game:  Burnout 3: Takedown(XBOX)                         Score: 94
Game:  Diablo(PC)                                        Score: 94
Game:  Metal Gear Solid 3: Subsistence(PS2)              Score: 94

截屏: 在此处输入图像描述


推荐阅读