tang7325 wrote:
https://finviz.com/screener.ashx
我想要抓finviz screener之後的資料, 不知我可以運用哪個範例來練習.
21樓的簡易範例,就可以處理
但這個網站,要一次全部匯出,是付費功能
不付費的情況下,只能一次一頁下載,以這個網站來說,要下載381次
測試不間斷連續下載6次
(381*6=2286次查詢)(每次下載7610筆資料,約2分半,每頁20筆約0.4秒)
目前看來似乎(暫時)沒有擋ip的功能
付費功能對您可能會比較方便,一次匯出通常不用1秒
免費要2分半,對網站做大量查詢,還有被擋ip的風險

'(點我看大圖)

Sub get_finviz()
Dim URL As String, HTML As Object, GetXml As Object, Table
Dim Page As Integer, LastRow As Integer, p As Integer, ttt As Double
Set HTML = CreateObject("htmlfile")
Set GetXml = CreateObject("msxml2.xmlhttp")
Application.ScreenUpdating = False
Cells.Clear
ttt = Timer
URL = "https://finviz.com/screener.ashx"
With GetXml
.Open "GET", URL, False
.send
HTML.body.innerhtml = .responsetext
Page = HTML.getElementById("pageSelect").Length
'first Page
Set Table = HTML.all.tags("table")(16).Rows
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
ActiveSheet.Cells(i + 1, j + 1) = Table(i).Cells(j).innertext
Next j
Next i
'2 ~ last page
For p = 2 To Page
LastRow = (p - 1) * 20 + 1
URL = "https://finviz.com/screener.ashx?v=111&r=" & LastRow
.Open "GET", URL, False
.send
HTML.body.innerhtml = .responsetext
Set Table = HTML.all.tags("table")(16).Rows
For i = 1 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
ActiveSheet.Cells(LastRow + i, j + 1) = Table(i).Cells(j).innertext
Next j
Next i
Next p
End With
Application.ScreenUpdating = True
Debug.Print "total page:" & Page & vbNewLine & "total time:" & Timer - ttt & _
vbNewLine & "Average per page:" & (Timer - ttt) / Page & "秒"
Set HTML = Nothing
Set GetXml = Nothing
End Sub