Python 雅虎金融抓取股票除了一个之外都可以工作

问题描述 投票:0回答:1

我尝试在雅虎财经上抓取股票价格,这段代码适用于很多股票代码,但股票代码“ISPA.DE”由于某种原因不起作用。它给出 AttributeError: 'NoneType' 对象没有属性 '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")

我尝试了多种方法,但没有结果。其他代码也可以工作...

python web-scraping yahoo-finance
1个回答
0
投票

尝试在请求中设置

User-Agent
HTTP 标头:

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")

打印:

26.63
© www.soinside.com 2019 - 2024. All rights reserved.