Xpath 如何在Excel/Google工作表中对网站进行Web抓取?
我应该如何刮这个网页,特别需要表中提到的ROE数字 我在Excel中使用了以下代码。我对谷歌的工作表刮削了解不多Xpath 如何在Excel/Google工作表中对网站进行Web抓取?,xpath,web-scraping,google-sheets,google-sheets-formula,google-sheets-importxml,Xpath,Web Scraping,Google Sheets,Google Sheets Formula,Google Sheets Importxml,我应该如何刮这个网页,特别需要表中提到的ROE数字 我在Excel中使用了以下代码。我对谷歌的工作表刮削了解不多 Sub FetchData() With ActiveSheet.QueryTables.Add(Connection:= _ "URL;https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/", Destination:=Range( _
Sub FetchData()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/", Destination:=Range( _
"$A$1"))
.Name = "www"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
我无法正确地获取数据
有什么建议/帮助吗?需要ROE数字,其余部分不是必需的。以下是我发现更容易获得特定值的方法。一旦循环的
检测到ROE
,它将跟踪所需的值并退出循环,因为它们都在同一父节点内
Sub FetchData()
Dim IE As New InternetExplorer, post As Object
Dim Html As HTMLDocument, elem As Object
With IE
.Visible = False
.navigate "https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
For Each post In Html.getElementsByTagName("td")
If post.innerText = "ROE" Then
Set elem = post.ParentNode.querySelector(".textvalue")
Exit For
End If
Next post
[A1] = elem.innerText
End Sub
以下是我发现更容易获得特定值的方法。一旦循环的检测到ROE
,它将跟踪所需的值并退出循环,因为它们都在同一父节点内
Sub FetchData()
Dim IE As New InternetExplorer, post As Object
Dim Html As HTMLDocument, elem As Object
With IE
.Visible = False
.navigate "https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
For Each post In Html.getElementsByTagName("td")
If post.innerText = "ROE" Then
Set elem = post.ParentNode.querySelector(".textvalue")
Exit For
End If
Next post
[A1] = elem.innerText
End Sub
不幸的是,这是不可能的,因为该站点由JavaScript控制,而Google Sheets无法理解/导入JS。您可以简单地通过禁用给定链接的JS来测试这一点,您将看到一个空白页面:
你所能得到的只是你所看到的:
=ARRAY_CONSTRAIN(IMPORTDATA("https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/"), 5000, 15)
不幸的是,这是不可能的,因为该站点由JavaScript控制,而Google Sheets无法理解/导入JS。您可以简单地通过禁用给定链接的JS来测试这一点,您将看到一个空白页面:
你所能得到的只是你所看到的:
=ARRAY_CONSTRAIN(IMPORTDATA("https://www.bseindia.com/stock-share-price/asian-paints-ltd/asianpaint/500820/"), 5000, 15)
使用API,页面使用它的速度要快得多。您可以使用powerquery来处理json响应、json解析器或仅使用split。如果您想在按下按钮时刷新,请将代码放入标准模块并链接到按钮
Option Explicit
Public Sub GetInfo()
Dim s As String, ids(), i As Long
ids = Array(500820, 500312, 500325, 532540)
With CreateObject("MSXML2.XMLHTTP")
For i = LBound(ids) To UBound(ids)
.Open "GET", "https://api.bseindia.com/BseIndiaAPI/api/ComHeader/w?quotetype=EQ&scripcode=" & ids(i) & "&seriesid=", False
.send
s = .responseText
ActiveSheet.Cells(i + 1, 1) = Split(Split(s, """ROE"":""")(1), Chr$(34))(0)
Next
End With
End Sub
使用API,页面使用它的速度要快得多。您可以使用powerquery来处理json响应、json解析器或仅使用split。如果您想在按下按钮时刷新,请将代码放入标准模块并链接到按钮
Option Explicit
Public Sub GetInfo()
Dim s As String, ids(), i As Long
ids = Array(500820, 500312, 500325, 532540)
With CreateObject("MSXML2.XMLHTTP")
For i = LBound(ids) To UBound(ids)
.Open "GET", "https://api.bseindia.com/BseIndiaAPI/api/ComHeader/w?quotetype=EQ&scripcode=" & ids(i) & "&seriesid=", False
.send
s = .responseText
ActiveSheet.Cells(i + 1, 1) = Split(Split(s, """ROE"":""")(1), Chr$(34))(0)
Next
End With
End Sub
缺少两个主要标记excel
和vba
。问题太广泛,因为它显然是在询问在两个电子表格应用程序上工作非常不同的功能。缺少两个主要标记excel
和vba
。问题太广泛,因为它显然是在询问工作非常不同的功能在两个电子表格应用程序上。我担心我应该如何添加这些参考资料?请查看以了解如何添加。非常感谢!我将对此进行更深入的研究。再次感谢你!再次非常感谢。对我来说简化了很多。SIM~它给了我运行时错误“91”:对象变量或块变量未设置。我担心我应该如何添加这些引用?请查看以了解如何添加。非常感谢!我将对此进行更深入的研究。再次感谢你!再次非常感谢。对我来说简化了很多。SIM~它给了我运行时错误“91”:对象变量或块变量未设置手动输入数据,我想,在一天结束时,自动化没有帮助。虽然我注意到他们改变了网站的功能。我确实很容易在网站上抓取其他东西,但这似乎很难破解。你提到的解释了一切。感谢手动输入数据,我想,在一天结束的时候,自动化是没有帮助的。虽然我注意到他们改变了网站的功能。我确实很容易在网站上抓取其他东西,但这似乎很难破解。你提到的解释了一切。谢谢谢谢这很有效,如果我想使用多个公司数据呢?我该怎么做?你会用一个循环。有哪些不同的公司?您需要将适当的id连接到api调用中。老实说,我不太喜欢VBA。就像我前面提到的,简单的东西是我可以做的。但我对这件事了解不多,例如500312、500325、532540,现在已经很清楚了。谢谢谢谢,这很有效,如果我想使用多个公司的数据呢?我该怎么做?你会用一个循环。有哪些不同的公司?您需要将适当的id连接到api调用中。老实说,我不太喜欢VBA。就像我前面提到的,简单的东西是我可以做的。但我对这件事了解不多,例如500312、500325、532540,现在已经很清楚了。谢谢