2013-10-11 63 views
1

我试图找到一种方式来获得yelp.com获取一个网站从列表数据到Excel VBA

我对其中有几个关键词和位置的电子表格中的数据。我正在寻找基于这些关键字和位置已经在我的电子表格中提取yelp列表中的数据。

我已经创建了下面的代码,但它似乎得到荒谬的数据,而不是我正在寻找的确切信息。

我想获得商家名称,地址和电话号码,但我所得到的只是一无所获。如果有人能帮我解决这个问题。

Sub find() 

Dim ie As Object 
    Set ie = CreateObject("InternetExplorer.Application") 
    With ie 
     ie.Visible = False 
     ie.Navigate "http://www.yelp.com/search?find_desc=boutique&find_loc=New+York%2C+NY&ns=1&ls=3387133dfc25cc99#start=10" 
     ' Don't show window 
    ie.Visible = False 

    'Wait until IE is done loading page 
    Do While ie.Busy 
     Application.StatusBar = "Downloading information, lease wait..." 
     DoEvents 
    Loop 

    ' Make a string from IE content 
    Set mDoc = ie.Document 
    peopleData = mDoc.body.innerText 
    ActiveSheet.Cells(1, 1).Value = peopleData 
End With 

peopleData = "" 'Nothing 
Set mDoc = Nothing 
End Sub 
+0

有你有机会尝试一下我的答案??? –

回答

5

如果你右击在IE,并做View Source,显而易见的是,在网站上提供的数据是不是文档的.Body.innerText财产的一部分。我注意到动态提供的数据通常会出现这种情况,而且这种方法对于大多数网络抓取来说太简单了。

我在Google Chrome中打开它并检查元素,以了解我真正在寻找什么,以及如何使用DOM/HTML解析器找到它;您将需要添加对Microsoft HTML对象库的引用。

enter image description here

我认为你可以得到它的返回<DIV>标签的集合,然后检查那些类名与环内的If statment。

我做了一些修改,以我原来的答复,这应该打印每条记录在一个新的小区:

Option Explicit 
Private Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 
Sub find() 
'Uses late binding, or add reference to Microsoft HTML Object Library 
' and change variable Types to use intellisense 
Dim ie As Object 'InternetExplorer.Application 
Dim html As Object 'HTMLDocument 
Dim Listings As Object 'IHTMLElementCollection 
Dim l As Object 'IHTMLElement 
Dim r As Long 
    Set ie = CreateObject("InternetExplorer.Application") 
    With ie 
     .Visible = False 
     .Navigate "http://www.yelp.com/search?find_desc=boutique&find_loc=New+York%2C+NY&ns=1&ls=3387133dfc25cc99#start=10" 
     ' Don't show window 
     'Wait until IE is done loading page 
     Do While .readyState <> 4 
      Application.StatusBar = "Downloading information, Please wait..." 
      DoEvents 
      Sleep 200 
     Loop 
     Set html = .Document 
    End With 
    Set Listings = html.getElementsByTagName("LI") ' ## returns the list 
    For Each l In Listings 
     '## make sure this list item looks like the listings Div Class: 
     ' then, build the string to put in your cell 
     If InStr(1, l.innerHTML, "media-block clearfix media-block-large main-attributes") > 0 Then 
      Range("A1").Offset(r, 0).Value = l.innerText 
      r = r + 1 
     End If 
    Next 

Set html = Nothing 
Set ie = Nothing 
End Sub 
+1

这是一个[忙碌的等待循环](http://stackoverflow.com/a/19019200/1768303),如果处理'ie_DocumentComplete'是不可能的,考虑在里面加入'Sleep(delay)'。 – Noseratio

+0

@Noseratio我刚刚注意到,实际上,并将循环更改为'Do While .readyState <> 4',也对代码做了一些调整以成为完美的解决方案。 –

+0

嗯,我没有看到变化。我的意思是像'DoEvents:Sleep(200)'(如果这是VBA,首先需要声明子睡眠库“kernel32”Alias“Sleep”(ByVal dwMilliseconds As Long)'),所以它不仅仅是在等待时吃掉CPU。一般来说,'DoEvents'可能导致重入问题,这里有一个很好的解释[为什么](http://stackoverflow.com/a/5183623/1768303)。 – Noseratio