从一堆网址中提取数据

时间:2015-03-30 09:01:32

标签: excel vba

vba新手。刚开始学习。我想从网站上提取一些特定数据。我想修改的代码来自Ron Retrieving specific data from website through excel

现在这段代码适用于单个网址。我在excel表的A列中有网址,我想要宏来逐一查看所有网址并分别将结果粘贴到B列C D中。

尽我最大限度的尝试。

此致

1 个答案:

答案 0 :(得分:-1)

主要子将获得A列中每个URL的评级和评论数量,并将它们放在B列和C列中。我希望这对您有所帮助。

Sub main()
Dim l As Long
l = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To l
test Range("A" & i)
Next
End Sub

Sub test(URL As Range)
    my_url = URL.Value
    Set html_doc = CreateObject("htmlfile")
    Set xml_obj = CreateObject("MSXML2.XMLHTTP")

    xml_obj.Open "GET", my_url, False
    xml_obj.send
    html_doc.body.innerhtml = xml_obj.responseText
    Set xml_obj = Nothing

    Set Results = html_doc.body.getElementsByTagName("i")

    For Each itm In Results

        If InStr(1, itm.outerhtml, "star-img", vbTextCompare) > 0 Then
            numb_stars = itm.getAttribute("title")
            Exit For
        Else
        End If
    Next
    Set Results = html_doc.body.getElementsByTagName("span")
    For Each itm In Results
        If InStr(1, itm.outerhtml, "reviewCount", vbTextCompare) > 0 Then
            numb_rev = itm.innertext
            Exit For
        Else
        End If
    Next

    URL.Offset(0, 1) = numb_stars
     URL.Offset(0, 2) = numb_rev
End Sub

预览我的输出:

enter image description here