如何通过 Selenium 获取股票代码? [英] How do I get the stock symbol by Selenium?

查看:46
本文介绍了如何通过 Selenium 获取股票代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个

附加信息:点击链接后,从绿色下拉列表中选择第二个,然后将显示上表.

解决方案

一种方式如下

from selenium import webdriverfrom selenium.webdriver.common.by import By从 selenium.webdriver.support.ui 导入 WebDriverWait从 selenium.webdriver.support 导入 expected_conditions 作为 EC将熊猫导入为 pdurl = 'https://stock360.hkej.com/StockScreener/profession/tab/profile'驱动程序 = webdriver.Chrome()driver.get(url)WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'option')))# 通过其值为 mb 的 value 属性选择第二个下拉选项driver.find_element_by_css_selector('[value=mb]').click()#等待蓝色按钮可点击并点击WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[href*=submit]'))).click()#选择表table = driver.find_element_by_css_selector('.dt960')#将表格的html传输到处理表格的pandas read_htmldf = pd.read_html(table.get_attribute('outerHTML'))[0] #抓取表格df2 = df.drop(df.columns[0], axis=1).dropna(how='all') #丢掉nan列和行df2.rename(columns=df.iloc[0], inplace = True) #设置标题与第 1 行相同df2.drop(df.index[0], inplace = True) #丢失第 1 行df2.reset_index(drop=True) #re-index打印(df2)驱动程序退出()

I am trying to grab the stock symbol from this page.

This is my code:

from selenium import webdriver
import pandas as pd

url = 'https://stock360.hkej.com/StockScreener/profession/tab/profile' 

browser = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
browser.get(url)

dfs = pd.read_html(browser.page_source)   
print(dfs)

browser.close()

This is the output:

dfs

[                     0
 0  加入至心水組合:請先登入或註冊成為會員, Empty DataFrame
 Columns: [沒有符合以上篩選條件的股票。]
 Index: [],                      0
 0  加入至心水組合:請先登入或註冊成為會員]

I know it's javascript and I used Selenium already. How come I can't get the table? And how do I get the stock symbol in the page as shown below? Thanks.

Additonal info: After clicking the link, choose the 2nd one from the GREEN drop-down list, then the above table will be shown.

解决方案

One way is as follows

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd

url = 'https://stock360.hkej.com/StockScreener/profession/tab/profile'
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'option')))
# select the second dropdown option by its value attribute whose value is mb
driver.find_element_by_css_selector('[value=mb]').click()
#wait for blue button to be clickable and click
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[href*=submit]'))).click()
#select table
table = driver.find_element_by_css_selector('.dt960')
#transfer html of table to pandas read_html which handles tables
df = pd.read_html(table.get_attribute('outerHTML'))[0] #grab the table
df2 = df.drop(df.columns[0], axis=1).dropna(how='all') #lose the nan column and rows
df2.rename(columns=df.iloc[0], inplace = True) #set headers same as row 1
df2.drop(df.index[0], inplace = True)  #lose row 1
df2.reset_index(drop=True) #re-index
print(df2)
driver.quit()

这篇关于如何通过 Selenium 获取股票代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆