Issue
I am making a program to scrape UFC fighters' names and info using BeautifulSoup
. I am using a for-loop
iterating through the div
holding this info scraping specific information.
The issue I am having is when printing the data only the first name is repeated as well as not displaying the correct amount of names. It repeats the name around ten times while there are definitely more than 11 names.
Current Code:
from cgitb import html
from bs4 import BeautifulSoup
import requests
html_text = requests.get('https://www.ufc.com/athletes/all').text
soup = BeautifulSoup(html_text, "lxml")
fighters = soup.find_all('div', class_ = ("node node--type-athlete node--view-mode-all-
athletes-result ds-1col clearfix"))
for fighter in enumerate(fighters, 1):
fighter_name = soup.find('span', class_ = ("c-listing-athlete__name")).text.replace("_",
" ")
fighter_nickname = soup.find('div', class_ = ("field field--name-nickname field--type-string field--label-hidden")).text
fighter_weight_class = soup.find('div', class_ = ("field field--name-stats-weight-class field--type-entity-reference field--label-hidden field__items")).text
fighter_ufc_record = soup.find('span', class_ = ("c-listing-athlete__record")).text
print(f'''Fighter Name: {fighter_name.strip()}''')
print("************************")
output:
fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
Fighter Name: Asjabharan
************************
When printing "fighter" I get all the data but I'm not able to manipulate the data neatly.
Solution
You have to use fighters in lieu of enumerate(fighters, 1) and fighter instead of soup
from cgitb import html
from bs4 import BeautifulSoup
import requests
html_text = requests.get('https://www.ufc.com/athletes/all').text
soup = BeautifulSoup(html_text, "lxml")
fighters = soup.find_all('div', class_ = ("node node--type-athlete node--view-mode-all-athletes-result ds-1col clearfix"))
for fighter in fighters:
fighter_name = fighter.find('span', class_ = ("c-listing-athlete__name")).text.replace("_", " ")
fighter_nickname = fighter.find('div', class_ = ("field field--name-nickname field--type-string field--label-hidden"))
fighter_nickname = fighter_nickname.text if fighter_nickname else None
fighter_weight_class = fighter.find('div', class_ = ("field field--name-stats-weight-class field--type-entity-reference field--label-hidden field__items")).text
fighter_ufc_record = fighter.find('span', class_ = ("c-listing-athlete__record")).text
print(f'''Fighter Name: {fighter_name.strip()}''')
print("************************")
Output:
Fighter Name: Asjabharan
************************
Fighter Name: Angga -
************************
Fighter Name: Danny Abbadi
************************
Fighter Name: Nariman Abbassov
************************
Fighter Name: Tank Abbott
************************
Fighter Name: Hamdy Abdelwahab
************************
Fighter Name: Shamil Abdurakhimov
************************
Fighter Name: Daichi Abe
************************
Fighter Name: Papy Abedi
************************
Fighter Name: Ricardo Abreu
************************
Fighter Name: Klidson Abreu
************************
Answered By - F.Hoque
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.