我想在python 3中以不同的方式合并两个列表

时间:2019-11-30 17:59:43

标签: python list

因此,我设法从有史以来最好的100场比赛中获得了数据,这些数据都是他们的头衔和得分得分,但是我不知道如何像先得分后再将冠军头衔一样将它们合并。我能得到一些帮助吗?

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)

2 个答案:

答案 0 :(得分:0)

您可以合并拥有的列表,然后使用zip

遍历它们。
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.text,'html.parser')

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

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


for game , score in zip(games,scores):
    game_title = game.text.replace("\n","").replace("   ","")
    score      = score.text
    print('game_title =>' , game_title , 'score =>' , score)

输出:

game_title =>  The Legend of Zelda: Ocarina of Time(N64)  score => 99
game_title =>  Tony Hawk's Pro Skater 2(PS)  score => 98
game_title =>  Grand Theft Auto IV(PS3)  score => 98
game_title =>  SoulCalibur(DC)  score => 98
game_title =>  Grand Theft Auto IV(X360)  score => 98
game_title =>  Super Mario Galaxy(WII)  score => 97

或者您可以找到具有名称和得分的父标记,如下所示:

items = soup.select('div.product_condensed div.product_wrap')

for item in items:
    name  = item.find('a').text.replace("\n","").replace("   ","")
    score = soup.find('div',{'class':'metascore_w small game positive'}).text
    print('Game_Name :' , name , 'Score:',score)

输出:

Game_Name :  The Legend of Zelda: Ocarina of Time(N64)  Score: 99
Game_Name :  Tony Hawk's Pro Skater 2(PS)  Score: 99
Game_Name :  Grand Theft Auto IV(PS3)  Score: 99
Game_Name :  SoulCalibur(DC)  Score: 99
Game_Name :  Grand Theft Auto IV(X360)  Score: 99
Game_Name :  Super Mario Galaxy(WII)  Score: 99

答案 1 :(得分:0)

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

截屏: enter image description here