如何从Instagram获得前50个帖子网址

时间:2017-11-14 04:47:56

标签: vba excel-vba internet-explorer excel

我想获得任何公共Instagram网址的前50个帖子。要获得默认可见的前12个帖子很容易,而且下面的代码就是这么做但我不确定如何访问其他在您点击底部的“加载更多”按钮时可见的网址并向下滚动更多。这可能吗?谁能帮我这个?

Sub getData()

Dim urL As String, instaID As String, totalPost As Long, fRow As Long
Dim ie, var1, var2, a

urL = "https://www.instagram.com/nike/"
OUT.Range("A2:A" & OUT.Rows.Count).Clear

Set ie = CreateObject("internetexplorer.application")
ie.Visible = True

fRow = 2

'----------------------
With OUT

    ie.navigate urL

    'Busy
    Do While ie.Busy
      Application.Wait DateAdd("s", 1, Now)
    Loop

    Set var1 = ie.document.getElementsByclassname("_mck9w _gvoze _f2mse")
    Do While IsObject(var1) = False
        Application.Wait Now + TimeValue("00:00:03")
        Set var1 = ie.document.getElementsByclassname("_mck9w _gvoze _f2mse")
    Loop

    Set var2 = var1(0).document.getElementsBytagname("a")

    For Each a In var2
        If Trim(a.href) <> "" And a.parentelement.classname = "_mck9w _gvoze _f2mse" Then

            .Range("A" & fRow) = a.href
            Debug.Print a.innertext & " " & a.parentelement.classname

             fRow = fRow + 1
        End If
    Next a


End With

'------------------
ie.Quit
Set ie = Nothing

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个

Sub getData()

    Dim urL As String, instaID As String, totalPost As Long, fRow As Long
    Dim ie As Object, var1 As Object, a As Variant

    urL = "https://www.instagram.com/nike/"

    Set ie = CreateObject("internetexplorer.application")
    ie.Visible = True

    ie.navigate urL

    Do While ie.Busy Or ie.readystate <> 4
        DoEvents
    Loop

    Do
        Set var1 = ie.Document.getElementsByclassname("_1cr2e _epyes")  ' wait for "load more"
    Loop While var1 Is Nothing

    var1(0).Click                                                       ' click "load more"

    Do While ie.Busy Or ie.readystate <> 4                              ' not sure if this is needed
        DoEvents
    Loop

    For a = 1 To 4
        ie.Document.parentWindow.scrollBy 0, 60000                      ' roll to bottom
        Application.Wait Now + TimeValue("00:00:01")                    ' wait for "fill in"
    Next a

    Set var1 = ie.Document.getElementsBytagname("a")

    Range("A2:A" & Rows.count).Clear
    fRow = 2

    For Each a In var1
        If Trim(a.href) <> "" And a.parentelement.classname = "_mck9w _gvoze _f2mse" Then
            Range("A" & fRow) = a.href
            Debug.Print a.innertext & " " & a.parentelement.classname
            fRow = fRow + 1
        End If
    Next a

    ' ------------------
    ie.Quit
    Set ie = Nothing

End Sub