Download data from WRDS using Python Selenium

閱讀時間約 7 分鐘
from selenium import webdriver
from time import sleep
import numpy as np
driver = webdriver.Chrome()
driver.get('https://wrds-www.wharton.upenn.edu/')
# Login WRDS
driver.find_element_by_id('id_username').clear()
driver.find_element_by_id('id_username').send_keys(ACCOUNT)
driver.find_element_by_id('id_password').clear()
driver.find_element_by_id ('id_password').send_keys(PASSWORD)
driver.find_element_by_xpath('//button[@class="btn btn-danger"]').click()
## -->CRSP
driver.find_element_by_xpath("//div[@class='col-md-4 col-lg-4'][1]//li[10]/a").click()
### -->stock files
driver.find_element_by_xpath("//div[@class='row m-bottom0'][2]/div[@class='col-md-4 col-lg-4'][1]//li[1]/a").click()
### -->daily
driver.find_element_by_xpath("//div[@class='col-md-4 col-lg-4'][1]//li[2]/a").click()
### -->TICKER FORMAT
driver.find_element_by_id('format-TICKER').click()
### -->all stocks in database
driver.find_element_by_id('search_option_method3').click()
### -->select columns
driver.find_element_by_id('select_all_button-80DBA1CF').click()
### -->output format
driver.find_element_by_id('csv').click()
## -->build date list
year = range(1930,1962)
year =[str(i) for i in year]
month = ['01','07']
list1 = []
list2 = []
for ix in year :
for iy in month :
list1.append( ix + '-' + iy +'-01')
list2.append( ix + '-' + str((int(iy)+5)).zfill(2)+'-'+str(30+int(iy)//6))
datelist = [list1,list2]
### --> for loop (change search dates)
datelist
for i in np.arange(39,41):
### -->modify end date before begin date, otherwise the begin date won't change
driver.find_element_by_id('select_end_date').clear()
driver.find_element_by_id('select_end_date').send_keys(datelist[1][i])
driver.find_element_by_id('select_beg_date').clear()
driver.find_element_by_id('select_beg_date').send_keys(datelist[0][i])
### -->submit query
driver.find_element_by_xpath('//button[@id="form_submit"]').click()
### -->switch to new page
driver.switch_to.window(driver.window_handles[1])
### -->click until 'csv' is prepared and download
temp=driver.find_element_by_xpath('//p[2]/a')
while not ('.csv' in temp.text):
sleep(5)
temp=driver.find_element_by_xpath('//p[2]/a')
driver.find_element_by_xpath('//p[2]/a').click()
### -->close new page and switch back to original page
driver.close()
driver.switch_to.window(driver.window_handles[0])
driver.switch_to.window(driver.window_handles[0])
len(datelist[0])
    留言0
    查看全部
    發表第一個留言支持創作者!
    Zhaung Eric的沙龍 的其他內容
    R example of Branching Ratio
    R example of Branching Ratio
    你可能也想看
    Google News 追蹤
    Thumbnail
    本專欄將提供給您最新的市場資訊、產業研究、交易心法、精選公司介紹,以上內容並非個股分析,還請各位依據自身狀況作出交易決策。歡迎訂閱支持我,獲得相關內容,也祝您的投資之路順遂! 每年 $990 訂閱方案👉 https://reurl.cc/VNYVxZ 每月 $99 訂閱方案👉https://re
    Thumbnail
    Unfamiliar with the new Minecraft mobile app? This guide covers everything you need to know, from downloading and installing the game to exploring in-
    Thumbnail
    立即訂閱!下載獨家AI Coser桌布!
    Thumbnail
    立即訂閱!下載獨家AI Coser桌布!
    Thumbnail
    下載《出版指南針:資深編輯給作者的創作建議》讓出書變成一件輕鬆的事情
    Thumbnail
    How to download torrent Chapter 1:What is a torrent downloader torrent chapter 2:How to download them? The End: Enjoy!
    Thumbnail
    如上篇,使用Migration來做DB版本控制,但是會發現開發過程中若是要建立測試資料,要進DB一筆一筆手動新增或執行預先寫好的insert sql,其實有點麻煩,使用Laravel提供的Seeder功能,就可以解決這個問題。 指令如下,假設建立一個user table seeder:
    Thumbnail
    如果想要在GridView控制項上套用DataTable.js的話有以下幾種方式。 一、純顯示用的GridView 由於DataTable.js使用上需要套在完整的table格式中, 使用GridView會沒有的部分,將無法套用。 這時請在後端你的Gridview DataBound區塊加入以下程式
    Thumbnail
    本專欄將提供給您最新的市場資訊、產業研究、交易心法、精選公司介紹,以上內容並非個股分析,還請各位依據自身狀況作出交易決策。歡迎訂閱支持我,獲得相關內容,也祝您的投資之路順遂! 每年 $990 訂閱方案👉 https://reurl.cc/VNYVxZ 每月 $99 訂閱方案👉https://re
    Thumbnail
    Unfamiliar with the new Minecraft mobile app? This guide covers everything you need to know, from downloading and installing the game to exploring in-
    Thumbnail
    立即訂閱!下載獨家AI Coser桌布!
    Thumbnail
    立即訂閱!下載獨家AI Coser桌布!
    Thumbnail
    下載《出版指南針:資深編輯給作者的創作建議》讓出書變成一件輕鬆的事情
    Thumbnail
    How to download torrent Chapter 1:What is a torrent downloader torrent chapter 2:How to download them? The End: Enjoy!
    Thumbnail
    如上篇,使用Migration來做DB版本控制,但是會發現開發過程中若是要建立測試資料,要進DB一筆一筆手動新增或執行預先寫好的insert sql,其實有點麻煩,使用Laravel提供的Seeder功能,就可以解決這個問題。 指令如下,假設建立一個user table seeder:
    Thumbnail
    如果想要在GridView控制項上套用DataTable.js的話有以下幾種方式。 一、純顯示用的GridView 由於DataTable.js使用上需要套在完整的table格式中, 使用GridView會沒有的部分,將無法套用。 這時請在後端你的Gridview DataBound區塊加入以下程式