Board logo

標題: [發問] 網站透過QueryTable下載的日期問題 [打印本頁]

作者: PKKO    時間: 2020-3-7 21:07     標題: 網站透過QueryTable下載的日期問題

各位大大好,小弟想透過VBA 的QueryTable 下載鉅亨網的股票資訊
網址如下:
https://www.cnyes.com/twstock/ps_historyprice/2327.htm

遇到的問題如下:
無法用程式碼控制起始日期.postText送出的資訊,無法正確查詢到我所指定的日期
但網站實際上是可以控制起始日期的
想請問各位大大要如何修改程式碼?

下載到(DL分頁)的程式碼如下:
  1. Sub test1()
  2. Dim QT As QueryTable
  3. Dim WebAddress As String
  4. WebAddress = "http://www.cnyes.com/twstock/ps_historyprice/2327.htm"
  5. With ThisWorkbook.Worksheets("DL")
  6.     .Cells.Clear
  7.     Set QT = .QueryTables.Add("URL;" & WebAddress, .Range("$A$1"))
  8. End With
  9. With QT
  10. .PostText = "ctl00$ContentPlaceHolder1$startText=" & " 2019/01/01" & "&ctl00$ContentPlaceHolder1$endText=" & " 2020/03/06"
  11. .WebTables = "1"
  12. .Refresh False
  13. .Delete
  14. End With
  15. End Sub
複製代碼

作者: Scott090    時間: 2020-3-8 12:02

回復 1# PKKO

試試看這個:
    Sub test1()
Dim QT As QueryTable
Dim WebAddress As String
    ' WebAddress = "http://www.cnyes.com/twstock/ps_historyprice/2327.htm"

WebAddress = "https://www.cnyes.com/twstock/ps_historyprice.aspx?"
WebAddress = WebAddress & "code=2327"
WebAddress = WebAddress & "&ctl00$ContentPlaceHolder1$startText=2019/01/01"
WebAddress = WebAddress & "&ctl00$ContentPlaceHolder1$endText=2020/03/06"

With ThisWorkbook.Worksheets("DL")
    .Cells.Clear
    Set QT = .QueryTables.Add("URL;" & WebAddress, .Range("$A$1"))
End With
With QT
   ' .PostText = "ctl00$ContentPlaceHolder1$startText=" & " 2019/01/01" & "&ctl00$ContentPlaceHolder1$endText=" & " 2020/03/06"
.WebTables = "1"
.Refresh False
.Delete
End With
End Sub
作者: PKKO    時間: 2020-3-8 14:13

回復 2# Scott090


    完全可以!!!

感謝大大

我都忘了可以從網址直接試試看
非常感謝

只是很奇怪,不知道.postText 錯在哪邊無法成功= =
不過可以用就很棒了,再次萬分感謝大大!!!
作者: Scott090    時間: 2020-3-8 14:28

本帖最後由 Scott090 於 2020-3-8 14:34 編輯

回復 3# PKKO

再測試如下:
       Sub test1()
Dim QT As QueryTable
Dim WebAddress As String
  '  WebAddress = "http://www.cnyes.com/twstock/ps_historyprice/2327.htm"

WebAddress = "https://www.cnyes.com/twstock/ps_historyprice.aspx?code=2327"
With ThisWorkbook.Worksheets("DL")
    .Cells.Clear
    Set QT = .QueryTables.Add("URL;" & WebAddress, .Range("$A$1"))
End With
With QT
     '.PostText = "ctl00$ContentPlaceHolder1$startText=" & " 2019/01/01" & "&ctl00$ContentPlaceHolder1$endText=" & " 2020/03/06"
'日期與等號間不要有空格
'======================
   .PostText = "ctl00$ContentPlaceHolder1$startText=" & "2019/01/01" & "&ctl00$ContentPlaceHolder1$endText=" & "2020/03/06"
.WebTables = "1"
.Refresh False
.Delete
End With
End Sub
作者: PKKO    時間: 2020-3-8 15:30

回復 4# Scott090


    原來如此
我認為是網址問題
因為我之前有把空格刪除過
感謝大大




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)