0

I'am trying to create a loop to show all values within the li tags to create a DataFrame. Moreover, I can only isolate the code using: new = soup.find("div", class_="PlayerList"). If I use a standard for loop it only shows one value not all values.

The output I would like to show is:

Messi,

Shooting 9,

Passing 9,

Tackle 4,

   <pre>
   import requests
   import pandas as pd
   import numpy as np

   from urllib.request import urlopen
   from bs4 import BeautifulSoup

   main_url = 'https://examplelistpython.000webhostapp.com/messi.html'
   result = requests.get(main_url)
   result.text

   soup = BeautifulSoup(result.text, 'html.parser')
   print(soup.prettify())

   new  = soup.find("div", class_="PlayerList")
   new

    </pre>

<ul class="List">
 <li>
  <div class="PlayerList">
   <div class="HeaderList">
    <span class="player">Messi</span>
</div>
  <div class="PlayerStat">
   <span class="stat">Shooting   <span class="allStatContainer statShooting" data-stat="Shooting">
     9
  </span>
 </span>
</div>
<div class="PlayerStat">
<span class="stat">Passing   <span class="allStatContainer statPassing" data-stat="Passing">
  9
 </span>
</span>
</div>
<div class="PlayerStat">
<span class="stat">Tackle   <span class="allStatContainer statTackle" data-stat="Tackle">
     4
     </span>
   </span>
  </div>
 </li>
</ul>
2
  • It that website working? It is showing error while using get() of requests Commented Mar 9, 2021 at 19:03
  • Yes. It should be working? Which code are you using? Commented Mar 9, 2021 at 19:08

1 Answer 1

1
player = [i.text.strip() for i in soup.find_all("span", class_="player")]
shooting = [i.text.strip() for i in soup.find_all("span", class_="allStatContainer statShooting")]
passing = [i.text.strip() for i in soup.find_all("span", class_="allStatContainer statPassing")] 
tackle = [i.text.strip() for i in soup.find_all("span", class_="allStatContainer statTackle")]

df = pd.DataFrame({'Player': player, 'Shooting': shooting, 'Passing': passing, 'Tackle': tackle})

Result:

Player Shooting Passing Tackle
0 Messi 9 9 4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.