以下是代码:
import requests
import bs4
response = requests.get('http://discoverygc.com/forums/serverinterface.php?action=players_online') #Loads page
soup = bs4.BeautifulSoup(response.text)
table = soup.find("div", {"id": "forum"})
rowsNo = (str(table).count('<tr>') - 2) #Number of players online. Minus 2 to remove leading title and column description rows
players = systems = [] #Define lists
for i in range(3, (rowsNo + 3)):
rows = table.findAll('tr')[i]
cols = rows.findAll('td')
player = cols[0].get_text()
system = cols[1].get_text()
players.append(player)
systems.append(system)
print(players)
print(systems)
如果删除players.append(player)
或systems.append(system)
代码,则代码正常工作并输出正确的列表:
['-Vasqez-', '[-=XTF=-]Neon.Bunny-[R]', "[SV]-Valley'", '<-JohnyWalker->', '~VP)Bad.Tibira', 'Alkanius', 'Apex91', 'Araroba', 'Baldor', 'Benediction', 'Black_Bird', 'Boost', 'Caelius.Moya[X]', 'Core|APM-Maverick', 'Daftwagen', 'Dee.Leers', 'Emiko:Hayashi', 'Gamma-6', 'Gauri', 'Gigi.7', 'GMG|GTS-Komahashi-Maru', 'Grawmod', 'GrazySlon', 'Hunor', 'Jakob-Schleiter', 'Joyita', 'Judge_BigJo', 'JulyJalwa', 'Kruger|KMS-Lankow', 'Luxor', 'monitor91', 'Morgulis', 'Nuggets', 'OSI-Mendes', 'Ronny.Rochester', 'Samura|-Arata', 'Samura|-Ichikawa', 'Shpritzen', 'Stardrifter', 'The_Altair', 'The.Liner.of.Dreams', 'Tony.Sosa', 'Wilde.RNC-Nestor']
或:
['Omega-11', 'Omega-49', 'Pennsylvania', 'Magellan', 'Omicron Gamma', 'Kyushu', 'Pennsylvania', 'Kyushu', 'Omega-5', 'Manchester', 'Cassini', 'Newcastle', 'Connecticut', 'Omega-47', 'Stuttgart', 'Stuttgart', 'Munich', 'New York', 'Hudson', 'Sigma-13', 'Languedoc', 'Colorado', 'Virginia', 'Stuttgart', 'New London', 'Magellan', 'New York', 'New Tokyo', 'Manchester', 'New York', 'Pennsylvania', 'Omega-3', 'Omega-49', 'New Berlin', 'California', 'Nagano', 'New Berlin', 'Okinawa', 'Magellan', 'Texas', 'Ontario', 'New Berlin', 'Stuttgart']
但是,如果我将两行放在一起,则将两者放在一起用于两个列表:
['-Vasqez-', 'Omega-11', "[SV]-Valley'", 'Omega-49', '<-JohnyWalker->', 'Pennsylvania', '=Z=Exositas', 'Magellan', '~VP)Death.Incarnator', 'Omicron Gamma', 'Alkanius', 'Shikoku', 'Apex91', 'Pennsylvania', 'Baldor', 'Kyushu', 'Benediction', 'Omega-5', 'Black_Bird', 'Manchester', 'Boost', 'Cassini', 'Caelius.Moya[X]', 'Connecticut', 'Core|APM-Maverick', 'Omega-47', 'Daftwagen', 'Stuttgart', 'Darf.Acour', 'Texas', 'Dee.Leers', 'New Berlin', 'Emiko:Hayashi', 'Munich', 'Gamma-6', 'New York', 'Gauri', 'Hudson', 'Gigi.7', 'Orkney', 'GMG|GTS-Komahashi-Maru', 'Colorado', 'Grawmod', 'Virginia', 'GrazySlon', 'Stuttgart', 'Hunor', 'Manchester', 'Jakob-Schleiter', 'New Berlin', 'Joyita', 'Magellan', 'Judge_BigJo', 'New York', 'Kruger|KMS-Lankow', 'New Tokyo', 'Luxor', 'Manchester', 'monitor91', 'New York', 'Morgulis', 'Pennsylvania', 'Nuggets', 'Omega-3', 'OSI-Mendes', 'Omega-49', 'Ronny.Rochester', 'California', 'Samura|-Arata', 'Nagano', 'Samura|-Ichikawa', 'New Berlin', 'Stardrifter', 'Okinawa', 'The_Altair', 'Magellan', 'Tony.Sosa', 'Ontario', 'Wilde.RNC-Nestor', 'Omega-7']
这是为什么?我看不出有什么理由发生这种情况。
答案 0 :(得分:6)
不要这样做,他们会引用相同的列表:
players = systems = [] #Define lists
但拆分他们:
players = []
systems = [] #Define lists
然后你将有两个单独的名单。
您的样式用于创建列表的名称别名(在某些情况下,它可能很有用),不来创建两个不同的列表
答案 1 :(得分:2)
您将players = systems = []
更改为单独分配。
players = []
systems = []
答案 2 :(得分:2)
如果你想把它保持在一行:
players, systems = [], []