exec85
exec85

Reputation: 487

cant get text from span with Beautifulsoup

Why can I not get the text 3.7M from those (multiple with same class name) span with below code?:

result_prices_pc = soup.find_all("span", attrs={"class": "pc_color font-weight-bold"})

HTML:

<td><span class="pc_color font-weight-bold">3.7M <img alt="c" class="small-coins-icon" src="/design/img/coins_bin.png"></span></td>

I try to get all prices with a for loop:

for price in result_prices_pc:
    print(price.text)

But I cant get the text from it.

Upvotes: 1

Views: 310

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195593

The "problem" is the pc_color CSS class. When you load the page, you need to specify what version of page do you need (PS4/XBOX/PC) - this is done by "platform" cookie (or you can use ps4_color instead of pc_color, for example):

import requests
from bs4 import BeautifulSoup

url = "https://www.futbin.com/players"
cookies = {"platform": "pc"}
soup = BeautifulSoup(requests.get(url, cookies=cookies).content, "html.parser")


result_prices_pc = soup.find_all(
    "span", attrs={"class": "pc_color font-weight-bold"}
)

for price in result_prices_pc:
    print(price.text)

Prints:

0 
1.15M 
3.75M 
1.7M 
4.19M 
1.81M 
351.65K 
0 
1.66M 
98K 
1.16M 
3M 
775K 
99K 
1.62M 
187K 
280K 
245K 
220K 
1.03M 
395K 
100K 
185K 
864.2K 
0 
1.95M 
540K 
0 
0 
89K 

Upvotes: 1

Prophet
Prophet

Reputation: 33371

These elements are actually having multiple class names: pc_color font-weight-bold are actually pc_color and font-weight-bold class names.
Forthis case you should use this syntax:

result_prices_pc = soup.find_all("span", attrs={"class": ['pc_color', 'font-weight-bold']})

Upvotes: 1

Related Questions