I would like to iterate on a Beautiful Soup object, but at the end it gives an error in find() because it is a list. How could I solve this error?

Asked

Viewed 39 times

0

import requests
from bs4 import BeautifulSoup
import pandas as pd


def get_object(url):
    
    soup = BeautifulSoup(requests.get(url).content,'html.parser')
    return soup


def get_all_links_players(bsObject):
    
    links_a = bsObject.find("td",class_ = "headcol no_sorter").findAll( "a")
    urls = [i.get("href") for i in links_a]
    return urls    

def get_links_players():
    
    links = ["https://www.mlb.com/stats" + "/?page" + "=" + str(i) for i in range(1,7)]
    result = [requests.get(i).text for i in links]
    bsObject = [BeautifulSoup(i,"lxml") for i in result]  
    all_players = [i.get("href") for i in bsObject.find("tbody",class_ = "notranslate").find_all("a")]
    return all_players
    
    
if __name__ == "__main__":
    
    for i in get_links_players():
        print(i)    
    

AttributeError: 'list' object has no attribute 'find'
  • The find is not set to list, in fact. You should apply find to each element in the list. If you exchange line 23 for all_players = [i.get("href") for i in bsObject[0].find("tbody",class_ = "notranslate").find_all("a")], the program prints the number of players for that element. But it’s not clear what you’re doing, so it’s hard to answer. If you can, explain it better

  • So, I’m making a web scraping of the baseball players from Mlb. I was able to iterate over all the player stats pages, so I want to take each player’s link and build a comic with all the details of each player.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.