excel 使用VBA从Web导入

bogh5gae  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(178)

bounty将在5天后过期。回答此问题可获得+50的声誉奖励。user3618585正在寻找来自声誉良好来源的答案

我在阅读下面代码的描述和价格时遇到问题。此代码从迈阿密Craigslist自动页面打开前4页,并将描述和价格读取到Excel。此代码成功打开前4页:

Sub fortesting()

    Dim link As HTMLLinkElement
    Dim blog As HTMLLinkElement
    Dim price As HTMLLinkElement
    Dim IE As Object
    Dim html  As HTMLDocument
    Dim URL As String
    Dim URLParameter As String
    Dim page As Long, counter As Long
    'Dim http As Object
    Dim links As Object
    Dim blogpost As Object
    Dim priceonly As Object
    Dim Results As Object
    Dim StartCell As Range
    Dim increment As Integer
    Dim htmlele1 As HTMLLinkElement
    Dim ss As Integer
    Dim ee As Integer
    Dim Index As Integer
    Dim k As Integer
    Dim q As Integer

    q = Format(k, "0")

   ' This is the first cell that a blog post hyperlink is created in
    Set StartCell = Range("A1")

    URL = "https://miami.craigslist.org/search/cta"
    Set IE = CreateObject("InternetExplorer.Application")

    Application.ScreenUpdating = True

    ' CHnage this to False if you want to hide IE
    IE.Visible = True

    counter = 0
    page = 0

    'Set the number of pages of the website to go through in the browser
    For page = 0 To 4 Step 1  'increment by 1 - total 4 pages
    Debug.Print page
   
    If page >= 0 Then URLParameter = "#search=1~list~" & page & "~0"

    IE.navigate URL & URLParameter
    
     'Wait for the browser to load the page
     Do Until IE.readyState = 4
    
        DoEvents
        
     Loop

     Set html = IE.document
     Set links = html.getElementsByTagName("h3")
     Index = 0

     For Each link In links
                        
        If InStr(LCase(link.outerHTML), "result-heading") Then
    
            Set blogpost = link.getElementsByTagName("title-blob")
            Set priceonly = link.getElementsByClassName("priceinfo")
            Set Results = html.getElementsByClassName("result-row")
            For Each blog In blogpost
                        
                StartCell.Offset(counter, 0).Hyperlinks.Add _
                Anchor:=StartCell.Offset(counter, 0), Address:=blog, _
                TextToDisplay:=link.innerText
                
                StartCell.Offset(counter, 1).Value = 
              Results(Index).getElementsByTagName("span")(0).innerText
                                    
                Index = Index + 1
            Next blog
            
           counter = counter + 1
            
        End If
    
        Next link
            
        Next page

        IE.Quit
        Set IE = Nothing
   
   
        Columns("B:B").Select
            Selection.NumberFormat = "$#,##0.00"
            Columns("D:D").Select
            Selection.NumberFormat = "m/d/yyyy;@"
     
      
   

         End Sub

任何帮助都是高度赞赏的。我只需要阅读说明和价格。例如:范围(“A1”)= 2008款凯迪拉克凯雷德范围(“B1”)= 8,500美元

eqzww0vc

eqzww0vc1#

如果你把这个函数放在一个模块中,它可以被调用来下载任何html(或任何其他来自网络的文件)-并在你的计算机上的“ToFile”中返回结果。

Function download(URL, ToFile) As Boolean
On Error GoTo feil
download = True
' Parameter for funsjon overføres
    strFileURL = URL                    ' eks "http://norsktipping.n3sport.no/default.aspx?event=GETMATCHINFO&NTMatchId=209562"
    strHDLocation = ToFile              ' eks "file.htm"
 
' Hent filen
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
 
    objXMLHTTP.Open "GET", strFileURL, False
    Sv = objXMLHTTP.send()
    
    If objXMLHTTP.Status = 200 Then
      Set objADOStream = CreateObject("ADODB.Stream")
      objADOStream.Open
      objADOStream.Type = 1 'adTypeBinary
 
      objADOStream.Write objXMLHTTP.ResponseBody
      objADOStream.Position = 0    'Set the stream position to the start
 
      Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
      Set objFSO = Nothing

      objADOStream.SaveToFile strHDLocation
      objADOStream.Close
      Set objADOStream = Nothing
    End If
 
    Set objXMLHTTP = Nothing
 Exit Function

feil:
If ActiveWorkbook.ReadOnly Then MsgBox ("Workbook is in read only mode! This result in error when using the dll for web.")
 
download = False
Resume Next
End Function

如果我对我们的URL进行测试,它会接受HTML,但不会接受js(Java Script)的结果,js会向网页返回InnerHtml元素。

Sub TestDownload()
'URL = "https://miami.craigslist.org/search/cta#search=1~list~1~0"
URL = "https://miami.craigslist.org/search/cta"
Sv = download(URL, "C:\Temp\TempHtmlFile.txt")

If Sv = True And Dir("C:\Temp\TempHtmlFile.txt") <> "" Then
FileNum = FreeFile
        Open "C:\Temp\TempHtmlFile.txt" For Input As #FileNum
        While Not EOF(FileNum)
            Line Input #FileNum, MyTxt ' Read one line to MyTxt and work with it in VBA
             MsgBox MyTxt
             MsgBox Len(MyTxt)
        Wend
        Close #FileNum
Else
MsgBox "Download of file failed"
End If

末端子组件

相关问题