2017-07-28 93 views
1

这里循环是从那里我试图返回的价格值仅链接:https://www.express-supp...VBA通​​过多个HTML表格

我有一个VBA脚本返回所有产品并网的细节表值代入工作簿,但其中一些值进入不允许生成枢轴数据透视表的错误列。但是,如果我更改此代码以生成名为价格框的表,它根本不返回任何值。

我认为页面上的HTML表格是乱序的,没有相互排序,这就是它使数据超出列的原因。作为解决方案,我希望VBA只返回页面的项目名称和价格,但不是全部。我该怎么做?的表是如何选择是否返回产品并网的细节返回到工作簿

例子: n/a

下面是代码:

 With CreateObject("WINHTTP.WinHTTPRequest.5.1") 
    .Open "GET", "https://www.express-supplements.co.uk/catalogsearch/result?q=Optimum+Nutrition", False 
    .send 
    oHtml.body.innerHTML = .responseText 
    Debug.Print 
End With 

ReDim a(1 To 100000, 1 To 60) 
For Each oElement In oHtml.getElementsByClassName("product-grid-details") 
    i = i + 1 
    x = Split(oElement.innerText, vbCr) 

    For ii = 1 To UBound(x) 
     a(i, 1) = nowDate 
     a(i, 2) = nowTime 
     a(i, 3) = weblinks(webX, 1) 
     a(i, 4) = weblinks(webX, 2) 
     a(i, ii + 4) = Trim$(x(ii)) 
    Next 

Next oElement 

    With SHwebdata 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
     .Cells(LastRow + 1, 1).Resize(i, UBound(a, 2)) = a 
     i = 0 
    End With 

回答

2

在这里你去。只需运行它,并按要求得到结果:

Sub Web_Data() 
    Dim http As New XMLHTTP60, html As New HTMLDocument 
    Dim topic As HTMLHtmlElement 

    With http 
     .Open "GET", "https://www.express-supplements.co.uk/catalogsearch/result?q=Optimum%20Nutrition", False 
     .send 
     html.body.innerHTML = .responseText 
    End With 

    For Each topic In html.getElementsByClassName("product-grid-details") 
     With topic.getElementsByClassName("product-name") 
      If .Length Then x = x + 1: Cells(x, 1) = .item(0).innerText 
     End With 
     With topic.getElementsByClassName("price") 
      If .Length Then Cells(x, 2) = .item(0).innerText 
     End With 
    Next topic 
End Sub 
+0

你是天才!完美地工作! – Martin

+0

请问你可以看看这个[link](http://www.bodybuildingwarehouse.co.uk/optimum-nutrition?limit=all)来查看是否有可能使用多个表格返回物品名称和价格你刚刚发布的方法相同吗?我认为它应该遵循** getElementsByClassName(“category-products”)**然后**(“product-name”)**和**(“price”)**,但由于某种原因它不起作用。可能是**。Item(0)**数字必须更改为返回值吗? – Martin

+0

没问题。只要打开一个新的线程,并给这里一个链接。我会看看那个。谢谢。 – SIM