简体   繁体   English

如何使用硒在网站上获取实时股价?

[英]How to use Selenium to get real-time stock price on website?

I am working on a school project to get the real-time stock price on JPMorgan Website . 我正在一个学校项目上,以获取摩根大通网站上的实时股价。 I would like to get all the data shown in <div class="price_area"> . 我想获取<div class="price_area">显示的所有数据。 I have tried beautifulsoup and yahoo api but still cannot get what I want. 我已经尝试了beautifulsoup和yahoo api,但仍然无法获得我想要的东西。 So, it my first time to try selenium but I have no idea how to run the javascript by it. 因此,这是我第一次尝试硒,但我不知道如何通过它运行javascript。 Here is my code: 这是我的代码:

def getStockPrice():
    driver = webdriver.Chrome()
    driver.get("http://www.jpmhkwarrants.com/en_hk/market-statistics/underlying/underlying-terms/code/1")
    try:
        stock_index = WebDriverWait(driver, 10).until(
            driver.find_element_by_class_name('price_area').find_element_by_class_name('price')
        )
    finally:
        driver.quit()

But it shows the error 'WebElement' is not callable. 但它显示错误“ WebElement”不可调用。 How can I get the real-time price, % change and the open price. 我如何获得实时价格,涨跌百分比和开盘价。 Thanks. 谢谢。

to use .find_element_by_* in WebDriverWait you have to use lambda function like 要在WebDriverWait使用.find_element_by_* ,您必须使用lambda函数,例如

stock_index = WebDriverWait(driver, 10).until(
     lambda d: d.find_element_by_class_name('price_area').find_element_by_class_name('price')
)

and don't forget to call .text to get the content 并且不要忘记调用.text来获取内容

def getStockPrice():
    driver = webdriver.Chrome()
    driver.get("http://www.jpmhkwarrants.com/en_hk/market-statistics/underlying/underlying-terms/code/0700")
    try:
        stock_index = WebDriverWait(driver, 10).until(
            lambda x: x.find_element_by_class_name('price_area')
        )
        price = stock_index.find_element_by_class_name('price')
        percentage = stock_index.find_element_by_css_selector('.percentage.rise')
        open_price = stock_index.find_element_by_css_selector('ul li span')

        print('Current price: ' + price.text)
        print('Percentage: ' + percentage.text)
        print('Open price: ' + open_price.text)

    except:
        print('wait timeout, element not found')
    finally:
        driver.quit()

You can use requests and BeautifulSoup to get the three items you mention using the Ajax query string call 您可以使用requestsBeautifulSoup通过Ajax查询字符串调用获取您提到的三个项目。

import requests
from bs4 import BeautifulSoup

url= 'http://www.jpmhkwarrants.com/en_hk/ajax/terms_quick_search?_=1543832902362'
res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml")
items = [item.text for item in soup.select('.price,.percentage.rise,li:nth-of-type(3) span')]
print(items)

Result: 结果:

在此处输入图片说明


The real time box has its own Ajax call of: 实时框具有自己的Ajax调用:

http://www.jpmhkwarrants.com/en_hk/ajax/market-terms-real-time-box/code/0700?_=1543832902364

You can use that to retrieve all items in that box. 您可以使用它来检索该框中的所有项目。

import requests
from bs4 import BeautifulSoup

url= 'http://www.jpmhkwarrants.com/en_hk/ajax/market-terms-real-time-box/code/0700?_=1543832902364'
res = requests.get(url)
soup = BeautifulSoup(res.content, "lxml")
items = [item.text.strip().split('\n') for item in soup.select('.price_area div')]
tidied = [item for sublist in items for item in sublist if item and item !='Change (%)']
print(tidied)

Result: 结果:

在此处输入图片说明

That data isn't real-time. 该数据不是实时的。

You have to pay for real time data usually. 通常,您必须为实时数据付费。

If your project involves any type of paper trading/analysis, know that everything you pull from a scrape will probably be delayed by 5-15min. 如果您的项目涉及任何类型的纸张交易/分析,请知道从刮擦中拉出的所有东西都可能会延迟5-15分钟。 I've heard Bloomberg has a free api, but I don't know if the real time data is free. 我听说彭博社有免费的API,但我不知道实时数据是否免费。 Check out Interactive Brokers API . 查阅Interactive Brokers API I'm pretty sure access to the data is free, and it allows you to connect to a paper trading account to test strategies, and algorithms. 我敢肯定,对数据的访问是免费的,它使您可以连接到模拟交易帐户来测试策略和算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM