新手求教 怎么从一个网站的多个网页中抓取信息。
我想从一个网站http://www.aloharag.com/eng的多个网页抓取信息,应该怎么设计。当我想抓取的description ,details and size 的 class name 都是“std"又该怎么抓取。
我在这里做了一点设计,试了一下,只能从一个页面抓取,比如这个设计只能抓到PRODUCT AND PRICE,请高手指点。多谢。
Sub test()
Dim eRow As Long
Dim ele As Object
Set sht = Sheets("Sheet1")
RowCount = 1
sht.Range("A" & RowCount) = " product"
sht.Range("B" & RowCount) = "price"
sht.Range("C" & RowCount) = "description"
sht.Range("D" & RowCount) = "details"
sht.Range("E" & RowCount) = "size"
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set objIE = CreateObject("InternetExplorer.Application")
mydesignername = InputBox("Enter name of designer")
With objIE
.Visible = True
.navigate " http://www.aloharag.com/eng"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set what = .document.getElementsByName("q")
what.Item(0).Value = mydesignername
.document.getElementsbyTagname(“button”)(0).click
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
For Each ele In .document.all
Select Case ele.classname
Case “item last”
RowCount = RowCount + 1
Case “product-name”
sht.Range("A" & RowCount) = ele.innertext
Case "price"
sht.Range("B" & RowCount) = ele.innertext
Case "std"
sht.Range("C" & RowCount) = ele.innertext
End Select
Next ele
End With
End Sub
[解决办法]
Private Sub CommandButton1_Click()
Set hf = CreateObject("htmlfile")
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", "http://www.aloharag.com/eng/catalogsearch/result/?order=relevance&dir=desc&q=SATURDAYS", False
.send
tt = .responsetext
b = Split(Split(tt, "<ul class=""products-grid"">")(1), "<script type")(0)
End With
hf.body.innerHTML = b
Set r = hf.all.tags("li")
For i = 0 To r.Length - 1
Cells(i + 2, 1) = r(i).all.tags("h2")(0).innerText
Cells(i + 2, 2) = r(i).all.tags("h3")(0).innerText
Cells(i + 2, 3) = r(i).all.tags("div")(0).innerText
Next i
End Sub
Private Sub CommandButton1_Click()
Set hf = CreateObject("htmlfile")
Set hf1 = CreateObject("htmlfile")
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", "http://www.aloharag.com/eng/catalogsearch/result/?order=relevance&dir=desc&q=SATURDAYS", False
.send
tt = .responsetext
b = Split(Split(tt, "<ul class=""products-grid"">")(1), "<script type")(0)
End With
hf.body.innerHTML = b
Set r = hf.all.tags("li")
For i = 0 To r.Length - 1
Cells(i + 2, 1) = r(i).all.tags("h2")(0).innerText
Cells(i + 2, 2) = r(i).all.tags("h3")(0).innerText
Cells(i + 2, 3) = r(i).all.tags("div")(0).innerText
Cells(i + 2, 4) = r(i).all.tags("h2")(0).all.tags("a")(0).href
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", r(i).all.tags("h2")(0).all.tags("a")(0).href, False
.send
hf1.body.innerHTML = .responsetext
Cells(i + 2, 5) = hf1.getElementById("product_tabs_description_contents").innerText
Cells(i + 2, 6) = hf1.getElementById("product_tabs_details_contents").innerText
Cells(i + 2, 7) = hf1.getElementById("product_tabs_size_contents").innerText
End With
Next i
End Sub