【问题标题】:VBA Script for scraping data doesn't work用于抓取数据的 VBA 脚本不起作用
【发布时间】:2013-11-29 14:25:40
【问题描述】:

我编写了一个简短的 VBA 脚本,它生成 URL 并下载页面内容并放入一个新的工作表。但是,数据始终显示在两个页面上,产生以下类型的 URL:

对于结果的第一页:

resultat_annuaire.php?loc=01&item=hopital&session=clear   (with 01 being the region) 

第二页:

resultat_annuaire.php?loc=01&item=hopital&page=2   (session=clear is gone, replaced by page=2) 

当我的 VBA 脚本生成并抓取 首页 的 URL 时,它可以正常工作(即,我将 95 个不同的页面下载到我的 excel 中)

但是,当我运行相同的 VBA 脚本(仅更改生成 URL 以获取第二个页面的方式)时,它下载的内容是第一个 URL 的第 2 页内容的 95 倍。

现在我尝试通过执行以下操作在我的网络浏览器中简单地摆弄 URL:

输入第二个页面 URL:

resultat_annuaire.php?loc=01&item=hopital&page=2

然后像这样将 01 更改为 05:

resultat_annuaire.php?loc=05&item=hopital&page=2

同样,没有任何反应,页面保持不变,即好像我没有将 01 切换到 05。

这是 VBA 脚本:

Sub Data_scraping()
    For x = 1 To 9
        ActiveWorkbook.Worksheets.Add
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;" _
            & "http://etablissements.hopital.fr/resultat_annuaire.php?loc=" _
            & "0" _
            & x _
            & "&item=hopital&session=clear" _
            , Destination:=Range("$A$1"))


        '.CommandType = 0


        .Name = "resultat_annuaire.php?loc=01&item=hopital&session=clear"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    ActiveWindow.SmallScroll Down:=18
    Rows("1:31").Select
    Selection.Delete Shift:=xlUp
    Range("A5").Select
Next x
End Sub

谁能提供解释或帮助?

【问题讨论】:

    标签: vba excel web-scraping web-crawler


    【解决方案1】:

    您的宏非常适用于 page=2

    Sub sof20287920Data_scrapping()
      Dim x, strLoc, strUrl
      Dim wkb
    
      Set wkb = Workbooks.Add()
      wkb.Activate
    
      For x = 1 To 9
        ActiveWorkbook.Worksheets.Add After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
        strLoc = "resultat_annuaire.php?loc=" & "0" & x & "&item=hopital&session=clear&page=2"
        strUrl = "http://etablissements.hopital.fr/" & strLoc
        With ActiveSheet.QueryTables.Add(Connection:= _
          "URL;" & strUrl _
          , Destination:=Range("$A$1"))
    
    
          '.CommandType = 0
    
    
          .Name = strLoc
          .FieldNames = True
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .PreserveFormatting = True
          .RefreshOnFileOpen = False
          .BackgroundQuery = True
          .RefreshStyle = xlInsertDeleteCells
          .SavePassword = False
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .WebSelectionType = xlEntirePage
          .WebFormatting = xlWebFormattingNone
          .WebPreFormattedTextToColumns = True
          .WebConsecutiveDelimitersAsOne = True
          .WebSingleBlockTextImport = False
          .WebDisableDateRecognition = False
          .WebDisableRedirections = False
          .Refresh BackgroundQuery:=False
        End With
    
        ActiveWindow.SmallScroll Down:=18
        Rows("1:31").Select
        Selection.Delete Shift:=xlUp
        Range("A5").Select
      Next x
    
    End Sub
    

    即使对于 page=2,session=clear 似乎也是必需的,如下所示:

    http://etablissements.hopital.fr/resultat_annuaire.php?loc=01&item=hopital&session=clear&page=2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多