代码之家  ›  专栏  ›  技术社区  ›  Beginner

如何使用fidelity的screener获取当前股价?

  •  0
  • Beginner  · 技术社区  · 3 年前

    我正试着用保真的屏幕来获取当前的股价。例如,AAPL的当前价格为 $165.02 在…上 https://digital.fidelity.com/prgw/digital/research/quote/dashboard/summary?symbol=AAPL

    当我检查网络空间时,价格如下所示: <div _ngcontent-cxa-c16="" class="nre-quick-quote-price">$165.02</div>

    我使用了以下代码:

    import requests
    from bs4 import BeautifulSoup
    
    def stock_price(symbol: str = "AAPL") -> str:
    
        url = f"https://digital.fidelity.com/prgw/digital/research/quote/dashboard/summary?symbol={symbol}"
        response = requests.get(url)
        soup = BeautifulSoup(response.text, "html.parser")
        price_tag = soup.find('div', class_='nre-quick-quote-price')
        current_price = price_tag['value']
    
        return current_price
    

    但出现了以下错误:

    Traceback (most recent call last):
      File "get_price.py", line 160, in <module>
        print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")
      File "get_price.py", line 63, in stock_price
        current_price = price_tag['value']
    TypeError: 'NoneType' object is not subscriptable
    

    我还使用了以下代码:

    from selenium import webdriver
    
    def stock_price(symbol: str = "AAPL") -> str:
    
        driver = webdriver.Chrome()
        url = "https://digital.fidelity.com/prgw/digital/research/quote/dashboard/summary?symbol=" + symbol
        driver.get(url)
    
        current_price = driver.find_element('div.nre-quick-quote-price').text
    
        return current_price
    

    但出现了以下错误:

    Traceback (most recent call last):
      File "get_price.py", line 103, in <module>
        price_tag = driver.find_element('div.nre-quick-quote-price')
      File "C:\Users\X\miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element
        return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
      File "C:\Users\X\miniconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
        self.error_handler.check_response(response)
      File "C:\Users\X\miniconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
    

    请帮忙!

    1 回复  |  直到 3 年前
        1
  •  1
  •   Shawn    3 年前

    问题的根本原因: 语法不正确。请参见下文。

    在第二个代码中,将以下行从更改为:

    current_price = driver.find_element('div.nre-quick-quote-price').text
    

    收件人:

    current_price = driver.find_element(By.CSS_SELECTOR, 'div.nre-quick-quote-price').text
    

    完整工作代码: 注:我添加了 explicitwaits 在您的代码中确保代码更加一致,这样即使网站响应/加载速度有点慢,您的代码也会处理它。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    
    def stock_price(symbol: str = "AAPL") -> str:
        driver = webdriver.Chrome()
        url = "https://digital.fidelity.com/prgw/digital/research/quote/dashboard/summary?symbol=" + symbol
        driver.get(url)
    
        wait = WebDriverWait(driver, 10)
        current_price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.nre-quick-quote-price'))).text
        return current_price
    
    print(stock_price())
    

    结果:

    $165.02
    
    Process finished with exit code 0
    

    更新了在无头模式下运行的代码:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    def stock_price(symbol: str = "AAPL") -> str:
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36")
        driver = webdriver.Chrome(options=options)
        url = "https://digital.fidelity.com/prgw/digital/research/quote/dashboard/summary?symbol=" + symbol
        driver.get(url)
    
        wait = WebDriverWait(driver, 10)
        current_price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.nre-quick-quote-price'))).text
        return current_price
    
    print(stock_price())