Reputation: 11
I try to scrape the 99damage top CS:GO player list and I have a problem with getting the player´s name.
<a href="http://csgo.99damage.de/edb/player/4151-krimz" class="item small">
<span stype="width:80px;"> 1. </span>
<span style="width: 280px;">
<img src="https://cdn1.gamesports.net/img/flags/se.gif" border="0" alt="se" title="Sweden">
" KRiMZ "
</span>
"KRiMZ" is the player´s name I try to get in this case, but there are many other names so i can´t just search for KRiMZ. I searched the web for solutions but didn´t find one that solves my problem.
Upvotes: 1
Views: 762
Reputation: 473763
I would locate every player element using a CSS selector (based on the player
part of the player profile URL) and then locate the first 2 spans inside - first one would be the rank, the second - username:
for player in soup.select("#content a[href*="player/"]"):
rank, name = player.find_all("span")[:2]
print(rank.get_text(strip=True), name.get_text(strip=True))
And here is the complete code I'm executing:
import requests
from bs4 import BeautifulSoup
url = "http://csgo.99damage.de/de/edb/players"
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"})
soup = BeautifulSoup(response.content, "html.parser")
for player in soup.select('#content a[href*="player/"]'):
rank, name = player.find_all("span")[:2]
print(rank.get_text(strip=True), name.get_text(strip=True))
Prints:
1. KRiMZ
2. olofmeister
3. JW
4. flusha
5. dennis
6. dev1ce
7. dupreeh
8. Xyp9x
9. Karrigan
10. cajunb
11. friberg
12. GeT_RiGhT
13. f0rest
14. Xizt
15. flamiE
16. GuardiaN
17. seized
18. Edward
19. Zeus
20. kioShiMa
21. pronax
22. Happy
23. NBK
Upvotes: 1