標題:
[發問]
股票歷史價格表
[打印本頁]
作者:
Scott090
時間:
2017-10-13 15:46
標題:
股票歷史價格表
進入這個網站取得個股歷史價:
http://www.cnyes.com/twstock/ps_historyprice/2330.htm
[attach]27803[/attach]
它只有固定的日期區間資料
請教導 :
VBA 如何用 選用其中的"開始日期"及"結束日期"及 "查詢" 而取得更長區間的資料表
謝謝
作者:
Scott090
時間:
2017-10-20 15:12
回復
1#
Scott090
參考以下程式碼可以設定日期區間
繼續尋求更佳的方法
'從鉅亨網取得歷史股價
'使用 IE 物件
'option explicit
'option base 1
Sub TestCNYES()
Dim Price(0 To 20, 0 To 9)
Dim Code
Code = "2330"
Dim IE As Object, URL$
Dim date0 As Date, StartDate$, EndDate$, timer, time0 As Date
Dim Table As Object, oDoc As Object, E
Dim Re%, Ce%, i%, k%, ColName$
ActiveSheet.Cells.Clear
'設定資料開始及結束日期
StartDate = Format(DateAdd("m", -2, Date), "yyyy/mm/dd") '取2個月的資料,約40筆
EndDate = Format(Date, "yyyy/mm/dd")
k = 0
Again:
Set IE = CreateObject("InternetExplorer.Application")
With IE
URL = "http://www.cnyes.com/twstock/ps_historyprice/" & Code & ".htm"
.Visible = False '不顯示 IE
.navigate URL
time0 = Now() + #12:00:05 AM# '設定時間控制計時5秒鐘
Do
DoEvents
Loop While (.Busy Or .readystate <> 4) And Time < time0
'網頁未準備好,關閉重啟
If .readystate <> 4 Then
.Quit
k = k + 1
If k <= 2 Then GoTo Again
GoTo GiveUp '開啟3次都失敗就放棄
End If
Set oDoc = .Document.getElementsByTagName("input")
With oDoc
.Item("ctl00$ContentPlaceHolder1$startText").Value = StartDate ' 開始日期 input name
.Item("ctl00$ContentPlaceHolder1$endText").Value = EndDate ' 結束日期 input name
Set E = .Item("ctl00$ContentPlaceHolder1$submitBut") '查詢鍵名稱
E.Click '按查詢鍵
End With
k = 0
time0 = Now() + #12:00:05 AM# '設定時間控制計時5秒鐘
Do
DoEvents
Loop While (.Busy Or .readystate <> 4) And Now() < time0
Application.Wait Time + #12:00:03 AM#
Set E = .Document.getElementsByTagName("TABLE")(1)
If E Is Nothing Then IE.Quit: GoTo Again
For Re = 0 To 19 '取19天的歷史紀錄
For Ce = 0 To 8
Price(Re, Ce) = E.Rows(Re).Cells(Ce).innertext '日期、開、高、低、收、漲跌 漲% 成交量 成交金額
Next
Next
ActiveSheet.Cells(1, "A").Resize(20, 9).Value = Price
GiveUp:
.Quit
End With
End Sub
複製代碼
作者:
iamaraymond
時間:
2018-1-29 01:15
回復
2#
Scott090
幫大大補充一下,
1.44~51行那邊應該可以用document.all(name)取代會比較簡單
2.可以透過修改
https://www.cnyes.com/twstock/ps_historyprice.aspx?code=2330&ctl00$ContentPlaceHolder1$startText=2001/10/05&ctl00$ContentPlaceHolder1$endText=2018/01/09
然後用外部匯入,會比較快,也比較簡單
個人小小想法,參考一下
作者:
Scott090
時間:
2018-1-29 06:19
回復
3#
iamaraymond
謝謝,我將試試看。
另,請教 Excel 2013 及它的 VBA 說明的查詢 是:
1.連線透過網路,
還是
2. 離線,已具備崁入(Built in)說明,只要按 F1 就可以
作者:
Scott090
時間:
2018-1-29 18:05
回復
3#
iamaraymond
依據大大的指導,修改如下,請看看是不是這樣子。
謝謝
'從鉅亨網取得歷史股價
'使用 IE 物件
'option explicit
'option base 1
Sub TestCNYES1()
Dim Price()
Dim Code
Code = "2330"
Dim IE As Object, URL$
Dim date0 As Date, StartDate$, EndDate$, submitBTN$, timer, time0 As Date
Dim Table As Object, oDoc As Object, E
Dim Re%, Ce%, i%, k%, ColName$
ActiveSheet.Cells.Clear
'設定資料開始及結束日期
StartDate = Format(DateAdd("m", -2, Date), "yyyy/mm/dd") '取2個月的資料,約40筆
EndDate = Format(Date, "yyyy/mm/dd")
StartDate = "&ctl00$ContentPlaceHolder1$startText=" & StartDate '開始日期 input name
EndDate = "&ctl00$ContentPlaceHolder1$endText=" & EndDate ' 結束日期 input name
' submitBTN = "&ctl00$ContentPlaceHolder1$submitBut=查詢"
k = 0
Again:
Set IE = CreateObject("InternetExplorer.Application")
With IE
URL = "https://www.cnyes.com/twstock/ps_historyprice.aspx?code=" & Code & StartDate & EndDate
.Visible = False '不顯示 IE
.navigate URL
time0 = Now() + #12:00:05 AM# '設定時間控制計時5秒鐘
Do
DoEvents
Loop While (.Busy Or .readystate <> 4) And Time < time0
'計時時間到了網頁未準備好,關閉重啟
If .readystate <> 4 Then
.Quit
k = k + 1
If k <= 2 Then GoTo Again
GoTo GiveUp '開啟3次都失敗就放棄
End If
' Set E = .Document.getElementsByTagName("TABLE")(0)
Set E = .Document.all.tags("TABLE")(0) '這個網頁的資料表有變動
If E Is Nothing Then IE.Quit: GoTo Again
i = E.Rows.Length - 1: k = E.Rows(1).Cells.Length - 1
ReDim Price(0 To i, 0 To k)
For Re = 0 To i '取19天的歷史紀錄
For Ce = 0 To k
Price(Re, Ce) = E.Rows(Re).Cells(Ce).innertext '日期、開、高、低、收、漲跌 漲% 成交量 成交金額
Next
Next
ActiveSheet.Cells(1, "A").Resize(i + 1, k + 1).Value = Price
GiveUp:
.Quit
End With
End Sub
複製代碼
作者:
iamaraymond
時間:
2018-1-29 19:49
回復
5#
Scott090
我的是按F1之後會上網找說明
另外我的想法是用QueryTable的方式
Sub 股價()
[A10].CurrentRegion.Clear
stockno = [B3]
mydate1 = Format([B4], "YYYY/MM/DD")
mydate2 = Format([B5], "YYYY/MM/DD")
myurl = "https://www.cnyes.com/twstock/ps_historyprice.aspx?code=" & stockno & "&ctl00$ContentPlaceHolder1$startText=" & mydate1 & "&ctl00$ContentPlaceHolder1$endText=" & mydate2
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & myurl _
, Destination:=Range("$A$10"))
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "1"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
.Delete
End With
End Sub
複製代碼
作者:
Scott090
時間:
2018-1-30 06:35
回復
6#
iamaraymond
" ..... F1上網找說明 .... ",就因為需網路連結這麼不方便,放棄使用。
謝謝大大的 QueryTable 模式。
因有其他程式碼的連結使用所以我直接把資料放進陣列
作者:
GBKEE
時間:
2018-1-30 12:46
本帖最後由 GBKEE 於 2018-1-30 12:47 編輯
回復
6#
iamaraymond
這網頁用
QueryTable 得取的資料有時起始日期會不準確,試試修改起始日期,看看資料是否正確
回復
7#
Scott090
Option Explicit
Sub Ex_Ie_Copy()
Dim Code As String, Price()
Dim date0 As Date, StartDate$, EndDate$, timer, time0 As Date, IE_StartDate As String
Dim E As Object, IE As Object
Code = "2330"
time0 = Time
Application.StatusBar = "開啟網頁...."
'設定資料開始及結束日期
StartDate = Format(DateAdd("m", -12 * 10, Date), "yyyy/mm/dd") '取10年資料筆
If StartDate < CDate("1994/09/07") Then
MsgBox "StartDate " & StartDate & vbLf & "不可小於" & "1994/09/07"
End
End If
EndDate = Format(Date, "yyyy/mm/dd")
Set IE = CreateObject("InternetExplorer.Application")
With IE
.navigate "http://www.cnyes.com/twstock/ps_historyprice/" & Code & ".htm"
Do: DoEvents: Loop While .Busy Or .readystate <> 4
With .Document.getElementsByTagName("input")
.Item("ctl00$ContentPlaceHolder1$startText").Value = StartDate ' 開始日期 input name
.Item("ctl00$ContentPlaceHolder1$endText").Value = EndDate ' 結束日期 input name
.Item("ctl00$ContentPlaceHolder1$submitBut").Click
End With
Do: DoEvents: Loop While .Busy Or .readystate <> 4
On Error Resume Next
Do
Err.Clear
Set E = .Document.getElementsByTagName("TABLE")(0)
If E.INNERTEXT <> "" Then
If Err = 0 Then
If Abs(DateValue(E.Rows(E.Rows.Length - 1).Cells(0).INNERTEXT) - DateValue(StartDate)) <= 15 Then
If Err = 0 Then Exit Do
End If
End If
End If
DoEvents
Application.StatusBar = " 等候 網頁資料中 ... "
Loop
On Error GoTo 0
CopyToClipbox E.OUTERHTML
Application.StatusBar = StartDate & " - " & EndDate & " 資料共 " & E.Rows.Length - 1 & " 讀取.." & Application.Text(Time - time0, ["m分:S秒 ok"])
.Quit
End With
End Sub
Private Sub CopyToClipbox(strText As String) '文本拷貝到剪貼板
With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.SetText strText
.PutInClipboard
End With
With ActiveSheet
.UsedRange.Clear
.[A1].Select
.Paste
End With
End Sub
複製代碼
Sub Ex_Ie()
Dim Code As String, Price()
Dim StartDate$, EndDate$, time0 As Date
Dim E As Object
Dim Re%, Ce%
Code = "2330"
time0 = Time
ActiveSheet.Cells.Clear
Cells(1).Activate
Application.StatusBar = "開啟網頁...."
'設定資料開始及結束日期
StartDate = Format(DateAdd("m", -12 * 10, Date), "yyyy/mm/dd") '取10年資料筆
If StartDate < CDate("1994/09/07") Then
MsgBox "StartDate " & StartDate & vbLf & "不可小於" & "1994/09/07"
End
End If
EndDate = Format(Date, "yyyy/mm/dd")
With CreateObject("InternetExplorer.Application")
.navigate "http://www.cnyes.com/twstock/ps_historyprice/" & Code & ".htm"
Do: DoEvents: Loop While .Busy Or .readystate <> 4
With .Document.getElementsByTagName("input")
.Item("ctl00$ContentPlaceHolder1$startText").Value = StartDate ' 開始日期 input name
.Item("ctl00$ContentPlaceHolder1$endText").Value = EndDate ' 結束日期 input name
.Item("ctl00$ContentPlaceHolder1$submitBut").Click
End With
Do: DoEvents: Loop While .Busy Or .readystate <> 4
On Error Resume Next
Do
Err.Clear
Set E = .Document.getElementsByTagName("TABLE")(0)
If E.INNERTEXT <> "" Then
If Err = 0 Then
If Abs(DateValue(E.Rows(E.Rows.Length - 1).Cells(0).INNERTEXT) - DateValue(StartDate)) <= 15 Then
If Err = 0 Then Exit Do
End If
End If
End If
DoEvents
Application.StatusBar = " 等候 網頁資料中 ... "
Loop
On Error GoTo 0
'ReDim Price(0 To E.Rows.Length - 1, 0 To E.Rows(0).Cells.Length - 1)
Application.StatusBar = StartDate & " - " & EndDate & " 資料共 " & E.Rows.Length - 2 & " 讀取...... "
For Re = 0 To E.Rows.Length - 1 ' 19 '取19天的歷史紀錄
For Ce = 0 To E.Rows(Re).Cells.Length - 1
Cells(Re + 1, Ce + 1) = E.Rows(Re).Cells(Ce).INNERTEXT '日期、開、高、低、收、漲跌 漲% 成交量 成交金額
' Price(Re, Ce) = E.Rows(Re).Cells(Ce).innertext '日期、開、高、低、收、漲跌 漲% 成交量 成交金額
Next
Next
'ActiveSheet.Cells(1, "A").Resize(UBound(Price), UBound(Price, 2)).Value = Price
Application.StatusBar = StartDate & " - " & EndDate & " 資料共 " & E.Rows.Length - 2 & " 讀取.." & Application.Text(Time - time0, ["m分:S秒 ok"])
.Quit
End With
End Sub
複製代碼
作者:
Scott090
時間:
2018-1-31 07:22
回復
8#
GBKEE
謝謝 GBKEE 大大 提供的:
資料完整性的查檢機制、 Clip Board 的方法
另, Do
.......
Application.StatusBar = " 等候 網頁資料中 ... "
Loop
中,有時不知甚麼狀況會有某些檔股票讀不到資料的情形,所以需要用時間計時來控制並放棄
再次感恩
作者:
Scott090
時間:
2018-1-31 21:49
回復
8#
GBKEE
沒有記錄下來,不過好像不是一個特定的;有時用手動直接去檢查又會在網頁上出現資料
下次碰到把它們記下來送給參考。
我在懷疑是網路本身的傳輸也說不定
謝謝
作者:
iamaraymond
時間:
2018-2-1 14:29
回復
8#
GBKEE
我記得會造成不一樣是因為如果輸入日期剛好碰到假日,那資料會以下一個工作日開始,例如輸入20180128(日),那就會抓到從20180129(一)開始的資料
作者:
GBKEE
時間:
2018-2-1 20:37
本帖最後由 GBKEE 於 2018-2-1 20:39 編輯
回復
11#
iamaraymond
這網頁可能也有流量管制
試試看
Option Explicit
Sub 股價()
Dim stockno As String, mydate1 As String, mydate2 As String, myurl As String
Dim Ar As Variant, i As Integer, xTime As Date
Ar = Array("2005/1/1", "2006/1/1", "2007/1/1", "2008/1/1")
[B3] = 2303
stockno = [B3]
[B5] = Date
For i = 0 To UBound(Ar)
[b4] = Ar(i)
[A10].CurrentRegion.Clear
mydate1 = Format([b4], "YYYY/MM/DD")
mydate2 = Format([B5], "YYYY/MM/DD")
myurl = "https://www.cnyes.com/twstock/ps_historyprice.aspx?code=" & stockno & "&ctl00$ContentPlaceHolder1$startText=" & mydate1 & "&ctl00$ContentPlaceHolder1$endText=" & mydate2
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & myurl, Destination:=Range("$A$10"))
.RefreshStyle = xlOverwriteCells
.SaveData = True
.AdjustColumnWidth = False
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "1"
Application.StatusBar = "資料下載中 ... " & [b4]
.Refresh BackgroundQuery:=False
.Delete
End With
With [A10].End(xlDown)
.Select
If Abs(.Cells - [b4]) > 15 Then
MsgBox "開始日期 " & [b4] & " <****> " & .Cells
End If
End With
If i < UBound(Ar) Then
xTime = Time
Do
DoEvents
Application.StatusBar = xTime + #12:00:20 AM# & " - 等候 20秒 ..." & Time
Loop While xTime + #12:00:20 AM# > Time
Else
MsgBox "程式 完畢 "
End If
Next
End Sub
複製代碼
作者:
Scott090
時間:
2018-2-2 08:58
回復
11#
iamaraymond
我想是資料的完整性問題,不是起始日期是否與資料日期一致
在下列位址有討論,請參考
http://forum.twbts.com/thread-20288-1-1.html
作者:
jackyq
時間:
2018-2-2 11:15
IE , ActiveX.EXE 最大特點是什? 善用之
作者:
Scott090
時間:
2018-2-3 06:48
回復
14#
jackyq
請 jackyq 大明示,以便學習
謝謝
作者:
jackyq
時間:
2018-2-3 16:44
回復
15#
Scott090
多工 ...
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)