【发布时间】:2019-06-09 17:14:16
【问题描述】:
我已经完成了我创建的这段代码,但我似乎遇到了一些问题。当您手动进入代码并运行它时,代码工作正常,但每次我尝试使用宏按钮自动运行代码时都会遇到问题。
我收到运行时错误“70”:权限被拒绝。我不确定为什么代码在我自动运行时会跳闸并抛出此代码。
这个想法是能够在 excel 中输入城镇和州,它将搜索这两个网站的数据。
我已经附上了下面的代码
'start a new subroutine called SearchBot
Sub SearchBot1()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim HTMLinputs As MSHTML.IHTMLElementCollection
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
'objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.zillow.com/orange-county-ny/home-values/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("local-search").Value = _
Sheets("Sheet2").Range("B3").Value & ", " & Sheets("Sheet2").Range("B4").Value
'click the 'go' button
Set HTMLinputs = objIE.document.getElementsByTagName("button")
For Each input_element In HTMLinputs
If input_element.getAttribute("name") = "SubmitButton" Then
input_element.Click
Exit For
End If
Next input_element
'wait again for the browser
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'price for home
Set Doc = objIE.document
Dim cclass As String
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(0).innerText)
'MsgBox (cclass)
Dim aclass As Variant
aclass = Split(cclass, " ")
Range("Market_Price").Value = aclass(0)
'1-YR Forecast
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(1).innerText)
'MsgBox (cclass)
Dim bclass As Variant
bclass = Split(cclass, " ")
Range("yr_forecast").Value = bclass(0)
'Median List Price
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(2).innerText)
'MsgBox (cclass)
Dim dclass As Variant
dclass = Split(cclass, " ")
Range("Median_List_Price").Value = dclass(0)
'Median Sale Price
cclass = Trim(Doc.getElementsByClassName("value-info-list")(0).Children(3).innerText)
'MsgBox (cclass)
Dim eclass As Variant
eclass = Split(cclass, " ")
Range("Median_Sale_Price").Value = eclass(0)
'Health of market
cclass = Trim(Doc.getElementsByClassName("value-info-list")(1).Children(0).innerText)
'MsgBox (cclass)
Dim fclass As Variant
fclass = Split(cclass, " ")
Range("Healthy").Value = fclass(0)
' Home with Negative Equity
cclass = Trim(Doc.getElementsByClassName("value-info-list")(1).Children(1).innerText)
'MsgBox (cclass)
Dim gclass As Variant
gclass = Split(cclass, " ")
Range("Home_With_Negative_Equity").Value = gclass(0)
'Delinquent on Mortgage
cclass = Trim(Doc.getElementsByClassName("value-info-list")(1).Children(2).innerText)
'MsgBox (cclass)
Dim hclass As Variant
hclass = Split(cclass, " ")
Range("Delinquent_On_Mortgage").Value = hclass(0)
'Listings with price cut
cclass = Trim(Doc.getElementsByClassName("value-info-list")(2).Children(2).innerText)
'MsgBox (cclass)
Dim iclass As Variant
iclass = Split(cclass, " ")
Range("Price_Cut").Value = iclass(0)
'Breakeven Horizon
cclass = Trim(Doc.getElementsByClassName("value-info-list")(3).Children(2).innerText)
'MsgBox (cclass)
Dim jclass As Variant
jclass = Split(cclass, " ")
Range("Breakeven").Value = jclass(0)
'Rent List Price
cclass = Trim(Doc.getElementsByClassName("value-info-list")(3).Children(3).innerText)
'MsgBox (cclass)
Dim kclass As Variant
kclass = Split(cclass, " ")
Range("Rent_List_Price").Value = kclass(0)
'Rent List Price/sq ft
cclass = Trim(Doc.getElementsByClassName("value-info-list")(3).Children(4).innerText)
'MsgBox (cclass)
Dim lclass As Variant
lclass = Split(cclass, " ")
Range("Rent_sq").Value = lclass(0)
'close the browser
objIE.Quit
Set ws = ThisWorkbook.Worksheets("Engine")
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
'objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://datausa.io/profile/geo/" & ws.Range("City_Search").Value & "-" & ws.Range("State_Search").Value
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set Doc = objIE.document
Dim Data As String
Data = Trim(Doc.getElementsByClassName("stat")(0).Children(1).innerText)
'MsgBox (Data)
Dim adata As Variant
adata = Split(Data, "")
ws.Range("Population").Value = adata(0)
End Sub
'exit our SearchBot subroutine
如果有人可以帮助我,将不胜感激。我将继续解决问题,看看我是否可以让它工作。如果您对公式有任何疑问,请提出。
谢谢
【问题讨论】:
标签: html vba excel web-scraping