Html Web抓取IE导航方法有效,而MSXML2.XMLHTTP60无效
我正在从NSE站点提取数据, 网址是: 我正在使用Internet explorer成功地提取项目,无论此方法多么缓慢, 所以我转到了MSXML2.XMLHTTP60方法,但该方法返回空字符串 请找到我的密码Html Web抓取IE导航方法有效,而MSXML2.XMLHTTP60无效,html,excel,vba,web-scraping,Html,Excel,Vba,Web Scraping,我正在从NSE站点提取数据, 网址是: 我正在使用Internet explorer成功地提取项目,无论此方法多么缓慢, 所以我转到了MSXML2.XMLHTTP60方法,但该方法返回空字符串 请找到我的密码 Method 1:Works fine Sub OI_Slow_Method() Dim ie As New InternetExplorer Set ie = CreateObject("InternetExplorer.Application") Dim Link As Strin
Method 1:Works fine
Sub OI_Slow_Method()
Dim ie As New InternetExplorer
Set ie = CreateObject("InternetExplorer.Application")
Dim Link As String
Link = ActiveSheet.Range("C4").Value
ie.Visible = False
ie.navigate Link
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = ie.document
Dim objElement As HTMLObjectElement
Dim sDD As String
doc.Focus
ActiveSheet.Cells(1, 1).Value = doc.getElementById("openInterest").innerText 'Open Interest Value
ie.Quit
ie.Visible = True
Set doc = Nothing
Set ie = Nothing
End Sub
'--------------------------
Method 2:Help required in this method only
Sub OI_Fast_Method()
Dim xhr As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
Set xhr = New MSXML2.XMLHTTP60
Set html = New MSHTML.HTMLDocument
With xhr
.Open "GET", "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&strike=-&expiry=30APR2020#", False
.send
html.body.innerHTML = StrConv(.responseBody, vbUnicode)
End With
Debug.Print html.getElementById("openInterest").Innertext
'The output of this is "<SPAN id=openInterest>??</SPAN>" only question mark returned inside the SPAN
End Sub
方法1:效果很好
子OI_慢_方法()
Dim ie成为新的InternetExplorer
设置ie=CreateObject(“InternetExplorer.Application”)
将链接设置为字符串
Link=ActiveSheet.Range(“C4”).值
可见=假
导航链接
做
多芬特
循环直到ie.readyState=readyState\u完成
作为HTMLDocument的Dim doc
Set doc=ie.document
作为HTMLObjectElement的Dim对象元素
将sDD设置为字符串
焦点医生
ActiveSheet.Cells(1,1).Value=doc.getElementById(“openInterest”).innerText的未平仓价值
即退出
可见=真实
设置文档=无
设置ie=无
端接头
'--------------------------
方法2:仅此方法中需要帮助
子OI_快速_方法()
Dim xhr为MSXML2.XMLHTTP60,html为MSHTML.HTMLDocument
设置xhr=New MSXML2.XMLHTTP60
Set html=New MSHTML.HTMLDocument
使用xhr
.打开“获取”https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&罢工=-&expiry=2020年4月30日#“,假
.发送
html.body.innerHTML=StrConv(.responseBody,vbUnicode)
以
Debug.Print html.getElementById(“openInterest”).Innertext
“此输出是”?“范围内返回的唯一问号
端接头
我认为蒂姆和往常一样一针见血。您得到的是一些原始XML,而您想要的东西不在该XML中。你可以做一个数据转储,得到你想要的
Sub DumpData()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&strike=-&expiry=28MAY2020#"
'Wait for site to fully load
ie.Navigate2 URL
Do While ie.Busy = True
DoEvents
Loop
RowCount = 1
With Sheets("Sheet1")
.Cells.ClearContents
RowCount = 1
For Each itm In ie.Document.all
.Range("B" & RowCount) = Left(itm.innerText, 1024)
RowCount = RowCount + 1
Next itm
End With
End Sub
然后您必须解析文本。这并不难,但会有一点额外的劳动
另一种选择可能是下载网站的全部内容,将其保存为文本文件,导入数据,然后解析该数据
Sub Sample()
Dim ie As Object
Dim retStr As String
Set ie = CreateObject("internetexplorer.application")
With ie
.Navigate "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&strike=-&expiry=28MAY2020#"
.Visible = True
End With
Do While ie.readystate <> 4: Wait 5: Loop
DoEvents
retStr = ie.document.body.innerText
'~> Write the above to a text file
Dim filesize As Integer
Dim FlName As String
'~~> Change this to the relevant path
FlName = "C:\Users\ryans\OneDrive\Desktop\Sample.Txt"
filesize = FreeFile()
Open FlName For Output As #filesize
Print #filesize, retStr
Close #filesize
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
子样本()
模糊的物体
作为字符串的Dim retStr
设置ie=CreateObject(“internetexplorer.application”)
与ie
.导航“https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&罢工=-&到期=2020年5月28日#”
.Visible=True
以
正在执行ie.readystate 4:等待5:循环
多芬特
retStr=ie.document.body.innerText
“~>将上述内容写入文本文件
将文件大小设置为整数
将名称设置为字符串
“~~>将此更改为相关路径
FlName=“C:\Users\ryans\OneDrive\Desktop\Sample.Txt”
filesize=FreeFile()
打开FlName以作为#文件大小输出
打印#文件大小,重新打印
关闭#文件大小
端接头
专用子等待(ByVal nSec长度)
nSec=nSec+定时器
而nSec>定时器
多芬特
温德
端接头
我无法让您的两个代码示例在我的机器上运行。我认为蒂姆像往常一样一针见血。您得到的是一些原始XML,而您想要的东西不在该XML中。你可以做一个数据转储,得到你想要的
Sub DumpData()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&strike=-&expiry=28MAY2020#"
'Wait for site to fully load
ie.Navigate2 URL
Do While ie.Busy = True
DoEvents
Loop
RowCount = 1
With Sheets("Sheet1")
.Cells.ClearContents
RowCount = 1
For Each itm In ie.Document.all
.Range("B" & RowCount) = Left(itm.innerText, 1024)
RowCount = RowCount + 1
Next itm
End With
End Sub
然后您必须解析文本。这并不难,但会有一点额外的劳动
另一种选择可能是下载网站的全部内容,将其保存为文本文件,导入数据,然后解析该数据
Sub Sample()
Dim ie As Object
Dim retStr As String
Set ie = CreateObject("internetexplorer.application")
With ie
.Navigate "https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&strike=-&expiry=28MAY2020#"
.Visible = True
End With
Do While ie.readystate <> 4: Wait 5: Loop
DoEvents
retStr = ie.document.body.innerText
'~> Write the above to a text file
Dim filesize As Integer
Dim FlName As String
'~~> Change this to the relevant path
FlName = "C:\Users\ryans\OneDrive\Desktop\Sample.Txt"
filesize = FreeFile()
Open FlName For Output As #filesize
Print #filesize, retStr
Close #filesize
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
子样本()
模糊的物体
作为字符串的Dim retStr
设置ie=CreateObject(“internetexplorer.application”)
与ie
.导航“https://www1.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=VOLTAS&instrument=FUTSTK&type=-&罢工=-&到期=2020年5月28日#”
.Visible=True
以
正在执行ie.readystate 4:等待5:循环
多芬特
retStr=ie.document.body.innerText
“~>将上述内容写入文本文件
将文件大小设置为整数
将名称设置为字符串
“~~>将此更改为相关路径
FlName=“C:\Users\ryans\OneDrive\Desktop\Sample.Txt”
filesize=FreeFile()
打开FlName以作为#文件大小输出
打印#文件大小,重新打印
关闭#文件大小
端接头
专用子等待(ByVal nSec长度)
nSec=nSec+定时器
而nSec>定时器
多芬特
温德
端接头
我无法在我的计算机上运行您的任何一个代码示例。当您使用IE(或任何浏览器)导航到某个页面时,该页面可能包含进一步向页面添加内容的脚本(通过从页面中嵌入的数据构建元素,或通过从服务器请求其他数据)。当您使用XmlHttp时,不会发生这种情况—您得到的只是服务器提供的原始页面源:没有其他内容—没有图像、脚本等。当您使用IE(或任何浏览器)导航到页面时,该页面可能包含进一步向页面添加内容的脚本(通过从页面中嵌入的数据构建元素,或者通过从服务器请求额外的数据)当您使用XmlHttp时,不会发生这种情况—您得到的只是服务器提供的原始页面源:没有其他内容—没有图像、脚本等。