Search code examples
pythonpython-3.xweb-scrapingbeautifulsoupyahoo-finance

Scraping change price of stocks in BeautifulSoup


I'm trying to scrape the change price of some stocks.

I tried this:

import requests
from bs4 import BeautifulSoup

url = requests.get('https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch').text
soup = BeautifulSoup(url, 'lxml')
ChangePrice = soup.find('span', {'class': 'Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($positiveColor)'}).text
print(ChangePrice)

Output: +1.59 (+0.36%)

.

As u can see at the end of ChangePrice variable it is $positiveColor, my problem is when I put a losing stock I have to change this to $negativeColor to make it work fine, is there any solution to make it works with both stocks the Positive color and the Negative color without changing code every time?

.

I tried to remove C($positiveColor) but it gives me an error AttributeError: 'NoneType' object has no attribute 'text' .

I wish my problem is clear, if anyone can help me I will be so appreciated.

.

Thanks in advance


Solution

  • You can add both classes in a list to select either one.

    import requests
    from bs4 import BeautifulSoup
    
    url = requests.get('https://finance.yahoo.com/quote/AAPL?p=AAPL&.tsrc=fin-srch').text
    
    soup = BeautifulSoup(url, 'lxml')
    ChangePrice = soup.find('span', {'class': ['Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($positiveColor)',
                                               'Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)']}).text
    
    print(ChangePrice)