Reputation: 25
I'm making a simple parser, which is taking some information and putting it into the variable (heroes_list
). So, using it outside the loop is giving me only last value, not all of them. How can I use heroes
variable outside the loop? (Inside the loop I get heroes_list = Abaddon, Alchemist..... Zeus
, but outside the loop i get only Zeus
. How to get Abaddon... Zeus
outside the loop)? Here's the code:
import requests
import fake_useragent
from bs4 import BeautifulSoup as BS
import itertools
response = requests.get('https://www.dotabuff.com/heroes', headers = {'User-agent': fake_useragent.UserAgent().random})
soup = BS(response.content, 'html.parser')
for name in soup.select(".name"):
try:
heroes = (str(name.text))
heroes_list = list(heroes.split())
print(*heroes_list)
except requests.exceptions.ConnectionError:
print("connection problem")
print(*heroes_list)
Upvotes: 0
Views: 87
Reputation: 5
what you need to do is create the variable as an array outside the loop and inside the for loop add the values to it with append, then display each of the values
heroes_list = [];
for name in soup.select(".name"):
try:
heroes = (str(name.text))
heroes_list.append(heroes)
except requests.exceptions.ConnectionError:
print("connection problem")
print(*heroes_list)
Upvotes: 0
Reputation: 132
You can create a list before the loop and keep appending to it:
import requests
import fake_useragent
from bs4 import BeautifulSoup as BS
import itertools
response = requests.get('https://www.dotabuff.com/heroes', headers = {'User-agent': fake_useragent.UserAgent().random})
soup = BS(response.content, 'html.parser')
heroes = []
for name in soup.select(".name"):
try:
heroes += (str(name.text)).split()
except requests.exceptions.ConnectionError:
print("connection problem")
print(*heroes_list)
Upvotes: 0
Reputation: 1687
you didn't declare the list before the loop started, try this:
import requests
import fake_useragent
from bs4 import BeautifulSoup
response = requests.get('https://www.dotabuff.com/heroes', headers = {'User-agent': fake_useragent.UserAgent().random})
soup = BeautifulSoup(response.content, 'html.parser')
heroes = [hero.get_text() for hero in soup.find_all('div', class_='name')]
print(heroes)
OUTPUT:
['Abaddon', 'Alchemist', 'Ancient Apparition', 'Anti-Mage', 'Arc Warden', 'Axe', 'Bane', 'Batrider', 'Beastmaster', 'Bloodseeker', 'Bounty Hunter', 'Brewmaster', 'Bristleback', 'Broodmother', 'Centaur Warrunner', 'Chaos Knight', 'Chen', 'Clinkz', 'Clockwerk', 'Crystal Maiden', 'Dark Seer', 'Dark Willow', 'Dawnbreaker', 'Dazzle', 'Death Prophet', 'Disruptor', 'Doom', 'Dragon Knight', 'Drow Ranger', 'Earth Spirit', 'Earthshaker', 'Elder Titan', 'Ember Spirit', 'Enchantress', 'Enigma', 'Faceless Void', 'Grimstroke', 'Gyrocopter', 'Hoodwink', 'Huskar', 'Invoker', 'Io', 'Jakiro', 'Juggernaut', 'Keeper of the Light', 'Kunkka', 'Legion Commander', 'Leshrac', 'Lich', 'Lifestealer', 'Lina', 'Lion', 'Lone Druid', 'Luna', 'Lycan', 'Magnus', 'Marci', 'Mars', 'Medusa', 'Meepo', 'Mirana', 'Monkey King', 'Morphling', 'Naga Siren', "Nature's Prophet", 'Necrophos', 'Night Stalker', 'Nyx Assassin', 'Ogre Magi', 'Omniknight', 'Oracle', 'Outworld Destroyer', 'Pangolier', 'Phantom Assassin', 'Phantom Lancer', 'Phoenix', 'Primal Beast', 'Puck', 'Pudge', 'Pugna', 'Queen of Pain', 'Razor', 'Riki', 'Rubick', 'Sand King', 'Shadow Demon', 'Shadow Fiend', 'Shadow Shaman', 'Silencer', 'Skywrath Mage', 'Slardar', 'Slark', 'Snapfire', 'Sniper', 'Spectre', 'Spirit Breaker', 'Storm Spirit', 'Sven', 'Techies', 'Templar Assassin', 'Terrorblade', 'Tidehunter', 'Timbersaw', 'Tinker', 'Tiny', 'Treant Protector', 'Troll Warlord', 'Tusk', 'Underlord', 'Undying', 'Ursa', 'Vengeful Spirit', 'Venomancer', 'Viper', 'Visage', 'Void Spirit', 'Warlock', 'Weaver', 'Windranger', 'Winter Wyvern', 'Witch Doctor', 'Wraith King', 'Zeus']
Upvotes: 1