Python yahoo finance scraping stocks all work but one

76 Views Asked by At

I try to scrape stockprices on yahoo finance and the piece of code works on alot of stock tickers but stock ticker "ISPA.DE" does not work for some reason. it gives AttributeError: 'NoneType' object has no attribute 'text'

import requests
from bs4 import BeautifulSoup

def stockprice(stock_ticker):
    url = "https://finance.yahoo.com/quote/" + str(stock_ticker) + "?p=" + str(stock_ticker) + "&.tsrc=fin-srch"
    print(url)
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    price = soup.find('fin-streamer', {"class": 'Fw(b) Fz(36px) Mb(-4px) D(ib)'}).text
    print(price)

stockprice("ISPA.DE")

I tried several ways but with no result. Other tickers do work...

1

There are 1 best solutions below

1
Andrej Kesely On

Try to set User-Agent HTTP header in request:

import requests
from bs4 import BeautifulSoup


def stockprice(stock_ticker):
    url = (
        "https://finance.yahoo.com/quote/"
        + str(stock_ticker)
        + "?p="
        + str(stock_ticker)
        + "&.tsrc=fin-srch"
    )
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"
    }

    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, "html.parser")
    price = soup.find("fin-streamer", {"class": "Fw(b) Fz(36px) Mb(-4px) D(ib)"}).text
    print(price)


stockprice("ISPA.DE")

Prints:

26.63