VBA web刮刮问题

我正在构build一个VBA Web刮板来抓取一个物品的所有名称和一个物品的价格。 我对VBA很新,并且对这个查询的最终细节有问题。

我想要它访问这个刀网站,拿名字和价格,并将它们存储到表单上,移动到下一个项目,一旦完成在该页面上,继续下一页,直到所有项目已被刮。 有人可以指出我在完成这个程序时犯的错误吗?

Sub printnumbs() ' Dim i As Integer For i = 0 To 10 Range("D6").Offset(0, (i * 2)).Value = i + 2 Next i ' End Sub Sub scrape() Dim i2 As Integer Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True For i2 = 1 To 2 'Max is For i2 = 0 To 400 .navigate "https://www.knifecenter.com/kc_new/store_store.html?ttl=Hunting%20and%20Bushcraft%20Knives&desc=Hunting%20Bushcraft&wnWWWCAT_1datarq=video&wnWWWCAT_1datarq=notepad&s=" & (i2 + (i2 * 30)) Application.Wait (Now + TimeValue("0:00:02")) Do DoEvents Loop Until ie.readyState = READYSTATE_COMPLETE Dim doc As HTMLDocument Set doc = ie.document While ie.readyState <> 4 Wend On Error Resume Next Dim i As Integer For i = 0 To 29 Range("B9").Offset(i + (i2 * 30), (0)).Value = doc.getElementsByClassName("maincontent")(0).getElementsByClassName("product_listing")(0).getElementsByClassName("row-fluid")(0).getElementsByClassName("listing_item span4")(0).getElementsByClassName("price-row")(0).getElementsByClassName("left-col")(0).innerText Range("C9").Offset(i + (i2 * 30), (0)).Value = doc.getElementsByClassName("maincontent")(0).getElementsByClassName("product_listing")(0).getElementsByClassName("row-fluid")(0).getElementsByClassName("listing_item span4")(0).getElementsByClassName("price-row")(0).getElementsByClassName("product_name")(0).innerText Next i Next i2 ie.Quit Application.EnableEvents = True End With ' End Sub 

给这个一个镜头。 当有一个强大的方法在那里,为什么坚持IE浏览器。 获取所有数据的时间不应超过5秒钟:

 Sub Web_Data() Const URL As String = "https://www.knifecenter.com/kc_new/store_store.html?ttl=Hunting%20and%20Bushcraft%20Knives&desc=Hunting%20Bushcraft&wnWWWCAT_1datarq=video&wnWWWCAT_1datarq=notepad&s=" Dim http As New XMLHTTP60, html As New HTMLDocument, page As Long, row As Long Dim topic As HTMLHtmlElement For page = 1 To 151 Step 30 With http .Open "GET", URL & page, False .send html.body.innerHTML = .responseText End With For Each topic In html.getElementsByClassName("listing_item span4") With topic.getElementsByClassName("product_name") If .Length Then row = row + 1: Cells(row, 1) = .Item(0).innerText End With With topic.getElementsByClassName("our_price") If .Length Then Cells(row, 2) = .Item(0).innerText End With Next topic Next page End Sub 

引用添加到库中:

 1. Microsoft HTML Object Library 2. Microsoft XML, V6.0 ''Or whatever version you have 

为什么你需要自定义的VBA来从这样的网站获取数据。 自动执行该过程所需的代码在其他站点上不起作用(因为每个站点的设置都不相同)并且不值得开发和维护代码。 这绝对不是初学者的项目。

有时候,好的旧的复制和粘贴技巧就好了。 Go to web page, CTRL+A CTRL+C, Right Click in Excel, Paste Special, Text. Repeat. 完成后,删除不需要的行,或者制作简单的公式来确定哪些行包含您的数据。

以下是您发送的链接上所有6个页面的“名称+价格”。 我花了更多的时间来写出这个答案比得到数据

 "Bark River Knives Bushcrafter Fixed 3.25 inch CPM-154 Blade, Black Canvas Micarta Handles, Leather Sheath";219.95 "Bark River Knives Bushcrafter II Fixed 4.7 inch CPM-3V Blade, Antique Ivory Micarta Handles, Leather Sheath";239.95 "Bark River Knives Bushcrafter II Fixed 4.7 inch CPM-3V Blade, Black Canvas Micarta Handles, Leather Sheath";234.95 "Bark River Knives Bushcrafter II Fixed 4.7 inch CPM-3V Blade, Bocote Wood Handles, Leather Sheath";256.95 "Bark River Knives Bushcrafter II Fixed 4.7 inch CPM-3V Blade, Green Canvas Micarta Handles, Leather Sheath";239.95 "Bark River Knives Ultra-Lite Bushcrafter Fixed 3.25 inch CPM-3V Blade, Black Canvas Micarta Handles, Leather Sheath";188.95 "Bark River Knives Ultra-Lite Bushcrafter Fixed 3.25 inch CPM-3V Blade, Bocote Wood Handles, Leather Sheath";209.95 "Bark River Knives Ultra-Lite Bushcrafter Fixed 3.25 inch CPM-3V Blade, Green Canvas Micarta Handles, Leather Sheath";196.95 "Benchmade 162-1 Bushcrafter Fixed 4.43 inch S30V Satin Blade, Sand G10 Handles, Kydex Sheath";182.75 "Boker Arbolito Dano Hunting Knife Fixed 8.5 inch N695 Blade, Guayacan Wood Handles, Leather Sheath";92.95 "Boker Arbolito El Gigante Hunting Knife Fixed 8.375 inch Stonewash Blade, Green Micarta Handles, Leather Sheath";146.95 "Boker Arbolito Nicker Hunting Knife 3-3/4 inch Fixed Blade, Stag Handles";82.36 "Boker Folding Hunting Knife 5.25 inch Closed, Stag Handles";194.95 "Boker Plus Bushcraft Next Generation Fixed 4 inch Drop Point Blade, Micarta Handles, Leather Sheath";59.96 "Boker Plus Bushcraft XL Fixed 4.375 inch Satin Blade, Orange G10 Handles";92.95 "Boker Plus Optima Hunting Set with Gut Hook, Saw Blade, Leather Sheath";51.95 "Boker Savannah Hunting Knife Fixed 4.625 inch N690 Two-Tone Blade, Micarta Handles, Leather Sheath";229.95 "Boker Savannah Hunting Knife Fixed 4.625 inch N690 Two-Tone Blade, Stag Handles, Leather Sheath";250.95 "Boker Savannah Hunting Knife Fixed 4.65 inch N690 Two-Tone Blade, Ram Horn Handles, Leather Sheath";236.95 "Browning Overtime Hunting Knife Fixed 3.6 inch D2 Satin Plain Blade, Green G10 Handles, Leather Sheath";86.95 "Browning Overtime Hunting Knife Fixed 3.6 inch D2 Satin Plain Blade, Tan G10 Handles, Leather Sheath";86.95 "Browning Speed Load Replaceable Hunting Blades, 50 Pack";28.95 "Buck 071 Kinetic Hunting Spearhead 2.5 inch Blade, Stainless Steel Handle";35.95 "Buck 119 Special 75th Anniversary Hunting Knife 6 inch Satin Blade, Black Phenolic Handle, Black Leather Sheath";59.95 "Buck 119BR Special 75th Anniversary Hunting Knife 6 inch Satin Blade, Cocobola Dymondwood Handle, Burgundy Leather Sheath";79.95 "Buck 120 General Hunting Knife Fixed 7-3/8 inch Blade, Black Phenolic Handles";76.95 "Buck 120 General Hunting Knife Fixed 7-3/8 inch Blade, Cocobola Dymondwood Handles";89.95 "Buck 191BR Zipper Hunting Knife Fixed 4-1/4 inch Blade with Gut Hook, Rubber Handle, Nylon Sheath";69.95 "Buck 191BR Zipper Hunting Knife Fixed 4-1/4 inch Blade with Gut Hook, Walnut Handle, Leather Sheath";85.95 "Buck 495 ErgoHunter Hunting Knife Fixed 4-3/4 inch 420HC Blade with Gut Hook, Alcryn Rubber Handles (0495BKG)";59.95 "Buck 673 BuckLite MAX Small Hunting Knife 3-1/4 inch 420HC Blade";18.95 "Buck 679 BuckLite MAX Large Hunting Knife 4 inch 420HC Blade";23.95 "Buck 679 BuckLite MAX Large Hunting Knife 4 inch 420HC Blade with Guthook";27.95 "Buck 679 BuckLite MAX Large Hunting Knife 4 inch Blade with Guthook, Orange Textured Alcryn Rubber Handles";29.95 "Buck 679 BuckLite MAX Large Hunting Knife 4 inch Blade, Orange Textured Alcryn Rubber Handles";23.95 "Camillus Bushcrafter USA Survival Fixed 4 inch 1095 Carbon Blade, Micarta Handle, Leather Sheath";77.95 "Camillus Carnivore 12 inch Titanium Bonded Fixed Blade Knife with Camo Handles";3 "Case Twin Finn Two Piece Hunting Set, Leather Handles, Leather Sheath";114.98 "Cold Steel 97GMS Gladius Machete 19 inch Blade, Polypropylene Handle, Cor-Ex Sheath";4.5 "Cold Steel 97KMS Kukri Machete 13 inch Blade, Polypropylene Handle, Cor-Ex Sheath";4.5 "Cold Steel 97MKM Magnum Kukri Machete 17 inch Blade, Polypropylene Handle, Cor-Ex Sheath";4 "Condor Tool & Knife CTK236-4HC Bushcraft Basic Camp Knife 4 inch Carbon Steel Black Blade, Hardwood Handle, Leather Sheath";50.98 "Condor Tool & Knife CTK236-5HC Bushcraft Basic Camp Knife 5 inch Carbon Steel Black Blade, Hardwood Handle, Leather Sheath";55.23 "Condor Tool & Knife CTK256-4.5HC Hivernant Hunting Knife 4-1/2 inch Satin Stainless Steel Blade, Hardwood Handles, Leather Sheath";43.95 "Condor Tool & Knife CTK410-14HCS Golok Machete 14 inch Black Carbon Steel Blade, Walnut Handles, Leather Sheath";4.5 "Condor Tool & Knife CTK412-17HCS Parang Machete 17-1/2 inch Carbon Steel Black Blade, Hardwood Handles, Leather Sheath";4.5 "Condor Tool & Knife CTK423-13HC Bushcraft Parang Machete 13 inch Carbon Steel Blade, Polypropylene Handles, Nylon Sheath";63.73 "Down Under Knives The Mistress Bowie Hunting Knife 13 inch Polished Blade, Leather Handle";249.95 "Down Under Knives The Outback Hunting Bowie Knife 11 inch Polished Blade, Leather Handle";199.95 "ESEE Knives Junglas Machete 10.38 inch Black Blade, Micarta Handles, Kydex Sheath";5 "Fremont Knives Farson Hunting Combo, Hatchet and Hide Glider Set";63.95 "Gerber Freeman Guide Hunting Knife Fixed 4 inch Bead Blast Plain Blade with Gut Hook, TacHide Handles, Nylon Sheath";20.76 "Gerber Freeman Guide Hunting Knife Fixed 4 inch Bead Blast Plain Blade, TacHide Handles, Nylon Sheath";20.76 "Gerber Moment Hunting Kit 2 Fixed Saw and Guthook Knife, Nylon Sheath";35.16 "Gerber Moment Hunting Kit 3 Fixed Saw, Caping and Guthook Knives, Nylon Sheath";59.95 "Grohmann Original Design Hunting Belt Knife 4 inch Blade, Rosewood Handles";89.95 "Grohmann Original Design Hunting Belt Knife 4 inch Blade, Stag Handles";124.76 "Havalon Hydra Double-Bladed Hunting Knife, 7 Blade Styles #60A, #70A, #22XT, #22, #127XT, #115XT and #115SW, Green Aluminum Handles";74.99 "Havalon Hydra Double-Bladed Hunting Knife, 7 Blade Styles #60A, #70A, #22XT, #22, #127XT, #115XT and #115SW, Red Aluminum Handles";74.99 "Havalon Titan Pro Replaceable Double-Bladed Hunting Knife, Gray Aluminum Handles with Black Inlays, Nylon Sheath";59.99 "Havalon Titan Replaceable Double-Bladed Hunting Knife, Black/Red Zytel Handles, Nylon Sheath";52.49 "Havalon Titan Replaceable Double-Bladed Hunting Knife, Blaze Orange Handles, Nylon Sheath";52.49 "Helle Brakar Hunting Knife 5 inch Blade, Curly Birch and Walnut Handle, Leather Sheath";149 "Helle Eggen Hunting Knife 4 inch Blade, Curly Birch Handle, Leather Sheath";109 "Helle Fjellkniven Hunting Knife 4 inch Blade, Curly Birch Handle, Leather Sheath";114 "Helle Harding Hunting Knife 4 inch Blade, Walnut and Curly Birch Handle, Leather Sheath";134 "Helle Lappland Hunting Knife 8-3/8 inch Blade, Birch Handle, Leather Sheath";169 "Helle Nying Hunting Knife 3 inch Blade, Curly Birch Handle, Leather Sheath";94 "Helle Odel Birch Hunting Knife 3-1/2 inch Blade, Stag Horn and Leather Handles, Leather Sheath";134 "Helle Symfoni Hunting Knife 3-1/2 inch Blade, Curly Birch, Stag Horn and Leather Handle, Leather Sheath";109 "Hubertus Hunting Cutlass 10.25 inch Satin Blade, Stag Handles with Silver Plating";507.96 "Hunting";Other Lock Types "KA-BAR 1249 Kukri Machete 11-1/2 inch Blade, Kraton G Handle, Leather / Cordura Sheath";4.5 "Kellam Knives Finnish Ranger Puukko M.95 Fixed Blade Forged Carbon Steel Hunting Knife";149.95 "Ken Onion Knifeworks Special Edition 6 inch Hunting/Fishing/Skinning/Fillet Knife, Polished G10 Handles, Leather Sheath, KnifeCenter Exclusive";139.95 "Kizlyar Supreme Echo Hunting Fixed 4.5 inch Niolox Satin Plain Blade, Black/White Micarta Handles, Kydex Sheath";125.95 "LionSteel M4 G10 Bushcrafting Fixed 3.74 inch Satin M390 Plain Blade, Black G10 Handles, Leather Sheath";155.99 "LionSteel M4 ST Bushcrafting Fixed 3.74 inch Satin M390 Plain Blade, Santos Wood Handles, Leather Sheath";155.99 "LionSteel M4 UL Bushcrafting Fixed 3.74 inch Satin M390 Plain Blade, Olive Wood Handles, Leather Sheath";155.99 "LionSteel M4 WN Bushcrafting Fixed 3.74 inch Satin M390 Plain Blade, Walnut Wood Handles, Leather Sheath";171.99 "Marttiini Lynx 121 Hunting Knife 3.54 inch Fixed Plain Blade, Curly Birch Handle, Leather Sheath";43.95 "Marttiini Lynx 131 Hunting Knife Fixed 4-1/4 inch Blade, Curly Birch Wood Handle";49.95 "Marttiini Lynx 139 Hunting Knife Fixed 5-1/8 inch Blade, Curly Birch Wood Handle";47.16 "Marttiini Ranger 250 Hunting Knife Fixed 6-1/4 inch Blade, Birch Wood Handle";95.95 "Maserin 181/CV Folding Hunting Knife 3 inch Polished Blade, Stag Handles";219.95 "Maserin 181/MC Folding Hunting Knife 3 inch Polished Blade, White and Blue Micarta Handles";159.96 "Maserin 976/G10A Croz Hunting Knife Fixed 4.375 inch N690 Satin Combo Blade, Orange G10 Handles, Black Nylon Sheath";144.95 "Maserin 976/G10N Croz Hunting Knife Fixed 4.375 inch N690 Satin Combo Blade, Black G10 Handles, Black Nylon Sheath";144.95 "Maserin 976/MCV Croz Hunting Knife Fixed 4.375 inch N690 Satin Combo Blade, Green G10 Handles, Black Nylon Sheath";144.95 "Morakniv Mora of Sweden Black Bushcraft Fixed 4.3 inch Carbon Steel Plain Blade, Black Rubberized Handle, MOLLE Sheath";89.99 "Morakniv Mora of Sweden Black Bushcraft Fixed 4.3 inch Stainless Steel Combo Blade, Black Rubberized Handle, MOLLE Sheath";89.99 "Morakniv Mora of Sweden Black Bushcraft Fixed 4.3 inch Stainless Steel Combo Blade, Black Rubberized Handle, Plastic Sheath";37.56 "Morakniv Mora of Sweden Black Bushcraft Fixed 4-1/4 inch Carbon Steel Blade, Black Rubberized Handle, Plastic Sheath";49.99 "Morakniv Mora of Sweden Black Bushcraft Fixed 4-1/4 inch Stainless Steel Combo Blade, Black Rubberized Handle, Plastic Sheath";49.99 "Morakniv Mora of Sweden Black Bushcraft Knife 4.3 inch Carbon Steel Blade, Black Rubber Handle";49.99 "Morakniv Mora of Sweden Black Bushcraft Pathfinder Fixed 6-3/4 inch Carbon Steel Blade, Black Rubberized Handle, MOLLE Compatible Heavy-Duty Nylon Sheath";89.99 "Morakniv Mora of Sweden Bushcraft Survival Knife 4-1/8 inch Black Carbon Steel Blade, Fire Starter and Sheath, Black Rubberized Handle";69.99 "Morakniv Mora of Sweden Forest Bushcraft Fixed 4 inch Stainless Steel Blade, Green Rubberized Handle, Plastic Sheath";37.95 "Morakniv Mora of Sweden Orange Bushcraft Fixed 4-1/4 inch Stainless Steel Blade, Orange Rubberized Handle, Plastic Sheath";37.95 "Ontario Bushcraft Field Knife Fixed 5 inch 5160 Carbon Blade, Walnut Wood Handles, Nylon Sheath";64.95 "Ontario Bushcraft Machete 16 inch 5160 Carbon Blade, Walnut Wood Handle, Nylon Sheath";74.95 "Ontario Bushcraft Machete 16 inch 5160 Carbon Blade, Walnut Wood Handle, Original DeSantis Nylon Sheath";98.95 "Ontario Bushcraft Machete Smooth Black Powdercoat 16 inch 5160 Carbon Blade, Walnut Wood Handle, DeSantis Nylon Sheath";99.95 "Ontario Bushcraft Utility Fixed 4 inch Carbon Steel Blade, Walnut Wood Handles, Nylon Sheath";64.95 "Ontario Bushcraft Utility Fixed 4 inch Carbon Steel Blade, Walnut Wood Handles, Original DeSantis Sheath";83.95 "Ontario Bushcraft Woodsman Fixed 9.18 inch 5160 Carbon Steel Blade, Walnut Wood Handles, Nylon Sheath";74.95 "Ontario Military Jungle Machete 18 inch Blade";4.5 "Ontario Nylon Sheath by DeSantis Gunhide Fits 6527 Bushcraft Utility";31.95 "Ontario Ranger Series Falcon Hunting Knife 3.875 inch Carbon Steel Blade, Micarta Handles, Nylon Sheath";64.95 "Ontario Ranger Series Falcon Hunting Knife 3-7/8 inch Carbon Steel Blade, Micarta Handles, Original Nylon Sheath";79.95 "Ontario Sawback Machete 18 inch Blade";4.5 "Outdoor Edge Dark Timber Skinner-Caper Hunting Knife Combo, Pakkawood Handles, Leather Sheath";49.99 "Outdoor Edge SwingBlade Hunting Knife, Two Blades in One, Black TPR Handle, Nylon Sheath";49.95 "Outdoor Edge SwingBlaze Hunting Knife, Two Blades in One, Orange TPR Handle, Nylon Sheath";49.95 "Outdoor Edge The Outfitter, 7 Piece Hunting Tool Set in Nylon Roll Case";92.95 "Outdoor Edge Zip-Blade Hunting Knife Fixed 4 inch Gutting Blade, Orange TPR Handles, Black Nylon Sheath";13.99 "RealSteel Knives Bushcraft II Fixed 4.125 inch D2 Plain Blade, Black G10 Handles, Kydex Sheath";54.8 "Reproduction Hunting Flintlock Dagger Pistol With Wood Grips";47.95 "Rockstead KEI-ZDP Japanese Hunting Knife Fixed 3.625 inch ZDP-189 Mirror Finish Blade, White Micarta Handles";0 "Schrade Bolo Machete 14 inch 3Cr13 Stainless Blade, Safe-T-Grip Handle, Ferro Fire Steel and Sharpening Stone";4.5 "Shadow Tech Gear2Survive Bushcraft Knife Fixed 4.38 inch Black Blade, Black Canvas Micarta Handles, Kydex Sheath";0 "Shadow Tech Gear2Survive Bushcraft Knife Fixed 4.38 inch Black Blade, Green Canvas Micarta Handles, Kydex Sheath";0 "Shadow Tech Gear2Survive Bushcraft Knife Fixed 4.38 inch Flat Dark Earth Blade, Black Textured G10 Handles, Kydex Sheath";0 "Shadow Tech Gear2Survive Bushcraft Knife Fixed 4.38 inch OD Green Blade, Black Textured G10 Handles, Kydex Sheath";0 "Spyderco FB26GP Bushcraft Survival Knife Fixed 4 inch Blade, Black G10 Handles";209.97 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Black G10 Handles, Kydex Sheath";174.95 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Black Linen Micarta Handles, Kydex Sheath";174.95 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Black/White G10 Handles";174.95 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Blue/Black G10 Handles";174.95 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Green/Black G10 Handles";174.95 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Red/Black G10 Handles";174.95 "TOPS Knives BOB Brothers of Bushcraft Fieldcraft Fixed 4.75 inch 154CM Blade, Tan Canvas Micarta Handles, Kydex Sheath";174.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Blade, Black G10 Handles";119.96 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Blade, Black Linen Micarta Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Blade, Brown Micarta Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Blade, Green/Black G10 Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Blade, Red/Black G10 Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Carbon Blade, White/Black G10 Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Stonewashed Blade, Brown Micarta Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Tan Blade, OD Green Micarta Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter 4-5/8 inch 1095 Tumbled Blade, Gray Linen Micarta Handles";149.95 "TOPS Knives BOB Brothers of Bushcraft Hunter Articulating Brown Leather Sheath, Ferro Rod, Survival Whistle";46.95 "TOPS Knives Nicholas Fury Bushcrafter Kukuri 7.0 Fixed 7-3/4 inch 1095 Kukri Blade, Micarta Handles (BKUK-01)";188.95 "TOPS Knives Wind Runner SRE Hunting Fixed Blade Combo Set, Green Micarta Handles, Black Kydex Sheath";209.95 "United Cutlery Bushmaster Bushcraft Primitive Field Knife 5.31 inch Drop Point Blade, Wood Handles, Leather Sheath";39.95 "United Cutlery Bushmaster Bushcraft Tactical Field Knife 5.31 inch Black Drop Point Blade, TPR Handles, Nylon Sheath";24.95 "United Cutlery Gil Hibben Master Bushcraft Machete 11.19 inch 1075 Plain Blade, Walnut Wood Handles, Nylon Sheath";78.95 "Utica Adirondack Hunting Knife Fixed 3-5/8 inch 420 Stainless Blade, Cocobola Wood Handles";49.95 "Utica Catskill Hunting Knife Fixed 2-3/4 inch 1095 Carbon Steel Blade, Brown Bone Handles";53.95 "Utica Catskill Hunting Knife Fixed 3-5/8 inch Carbon Steel Blade, Brown Jigged Bone Handles";46.36 

你知道如何从这里复制数据到Excel吗?

这个网站是不是最容易刮。 为了节省时间,我用class="listing_item span4"迭代了所有的div,并parsing了它们的innerText。

我build议你看这些video:

  • Excel VBA简介第5部分 – select单元(范围,单元,活动单元,结束,偏移)
  • Excel VBA简介第47部分 – 浏览网站和刮网页
  • Excel VBA简介第48部分 – 跨多页的网页刮

在这里输入图像描述

 Sub scrape() Dim URL As String Dim aNext As HTMLAnchorElement, div As HTMLDivElement, doc As HTMLDocument URL = "https://www.knifecenter.com/kc_new/store_store.html?ttl=Hunting%20and%20Bushcraft%20Knives&desc=Hunting%20Bushcraft&wnWWWCAT_1datarq=video&wnWWWCAT_1datarq=notepad&s=1" Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True Do IE.navigate URL Do DoEvents Loop Until IE.readyState = READYSTATE_COMPLETE Set doc = IE.document For Each div In doc.getElementsByClassName("listing_item span4") ProcessDiv div Next URL = "" On Error Resume Next URL = doc.getElementsByClassName("next")(0).href On Error GoTo 0 Loop Until Len(URL) = 0 IE.Quit Application.EnableEvents = True End Sub Private Sub ProcessDiv(div As HTMLDivElement) Dim Name As String Dim arr As Variant Dim Price As Double arr = Split(div.innerText, vbCrLf) Name = arr(4) Price = arr(8) AppendProduct Name, Price End Sub Private Sub AppendProduct(Name As String, Price As Double) With Worksheets("Sheet1") With .Range("A" & .Rows.Count).End(xlUp).Offset(1) .Cells(1, 1).Value = Name .Cells(1, 2).Value = Price End With End With End Sub