Excel 我想从网页中提取数据,但最后我无法在工作表中写入数据。数据是否为表格形式(多行、列)?

Excel 我想从网页中提取数据,但最后我无法在工作表中写入数据。数据是否为表格形式(多行、列)?,excel,vba,Excel,Vba,无法从网页中提取数据&在工作表中写入数据也是一个问题 子数据() "宣言", Dim IE As Object Dim doc As HTMLDocument '启动应用程序 Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True "导航", IE.navigate "https://nseindia.com/live_market/dynaContent/live_watch/option_chain/

无法从网页中提取数据&在工作表中写入数据也是一个问题

子数据()

"宣言",

Dim IE As Object
Dim doc As HTMLDocument
'启动应用程序

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
"导航",

IE.navigate "https://nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=25APR2019"
'循环直到加载页面

Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
'循环直到加载页面

Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
端接头


我想要nse工作表(工作表名称)中的表数据。

可能会尝试类似的方法,我只成功地尝试了漂亮的数据,因为我不知道基本符号,除了SBI的“SBIN”,即使在手动冲浪时,它也会给出“内部服务器错误-读取”页面(可能是因为周日)

可以尝试使用基本符号(可能在适当的时间)

代码:

Sub-pulldata()
"宣言",
模糊的物体
作为HTMLDocument的Dim doc
'启动应用程序
Dim Tbl为HTMLTable,Cel为HTMLTableCell,Rw为HTMLTableRow,Col为HTMLTableCol
变暗TrgRw为长,TrgCol为长
设置IE=CreateObject(“InternetExplorer.Application”)
可见=真实
"导航",
即“导航”https://nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=25APR2019"
'循环直到加载页面
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
'将数据放入网页并单击提交
Set doc=IE.document
'doc.getElementById(“underlyStock”).Value=ThisWorkbook.Sheets(“URLList”).Range(“A2”).Value
'doc.parentWindow.execScript“goBtnClick('stock');”,“javascript”
'循环直到加载页面
在忙或准备状态4时执行
Application.Wait DateAdd(“s”,1,Now)
环
'将数据从elementid-“octable”拉至工作表-“nse”
设置Tbl=doc.getElementById(“八进制”)
TrgRw=1
对于Tbl.行中的每个Rw
TrgCol=1
对于Rw.单元格中的每个单元格
ThisWorkbook.Sheets(“nse”).Cells(TrgRw,TrgCol).Value=Cel.innerText
如果列跨度大于1倍,则TrgCol=TrgCol+Cel.colSpan'
下一个细胞
TrgRw=TrgRw+1
下一个Rw
端接头

关于内部服务器错误,我与@AhmedAU提到的问题相同

如果此解决方案考虑使用来自Web工具的内置数据。工具>数据>来自Web>添加到包含符号的地址栏的url,例如

按开始。请稍等片刻,然后选择所需的表并导入。该表将导入Excel


在早期版本的Excel中,您可能需要从Microsoft下载免费的powerquery加载项,并使用powerquery选项卡执行相同的任务。

您是否在此处查看过类似的问题,其中有一些工作代码示例……先生,我试过了,但没有得到正确的答案。无论你找到什么答案,都需要根据你的情况进行编辑-不要期望它们是为你的需要而写的…你能提供一个基本股票价值的例子吗?无论ThisWorkbook.Sheets(“URLList”).Range(“A2”)的值是多少。页面似乎没有返回此列表中项目的任何信息:@QHarr,链接页面中的基础股票正常,但可能是服务器在交易时间以外(通常)没有完全工作。Ahmed AU sir非常感谢,代码工作得非常好。先生,您能在这个vba中帮助我吗-
Sub pulldata()
'declaration
Dim IE As Object
Dim doc As HTMLDocument
'start application
Dim Tbl As HTMLTable, Cel As HTMLTableCell, Rw As HTMLTableRow, Col As HTMLTableCol
Dim TrgRw As Long, TrgCol As Long



Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'navigation

IE.navigate "https://nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=25APR2019"
'loop until load page

Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
'put data in webpage and click on submit

Set doc = IE.document

'doc.getElementById("underlyStock").Value = ThisWorkbook.Sheets("URLList").Range("A2").Value
'doc.parentWindow.execScript "goBtnClick('stock');", "javascript"
'loop until load page

Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
'pull data from elementid- "octable" to sheet- "nse"

Set Tbl = doc.getElementById("octable")


TrgRw = 1
    For Each Rw In Tbl.Rows
    TrgCol = 1
        For Each Cel In Rw.Cells
        ThisWorkbook.Sheets("nse").Cells(TrgRw, TrgCol).Value = Cel.innerText
        TrgCol = TrgCol + Cel.colSpan   ' if Column span is > 1 multiple
        Next Cel
    TrgRw = TrgRw + 1
    Next Rw
End Sub