Excel vba如何抓取指定的网页数据到单元格

2024-05-19 01:46

1. Excel vba如何抓取指定的网页数据到单元格

参考:
Sub A1下载数据()
ReDim A2(1 To 200000, 1 To 15): A = 0
For i = 1 To 5
Sleep 2000 + 1000 * Rnd
With CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "目标网页" 
.Open "get", URL, False
.setRequestHeader "Host", "xxxxx"
.Send
Q1 = .responseText
Q1 = Replace(Q1, """", "")
Q1 = Replace(Q1, Chr(9), "")
Q1 = Replace(Q1, Chr(10), "")
Q1 = Replace(Q1, Chr(13), "")
Q1 = Replace(Q1, "=odd>", "=>")
End With
'Sheet1.[A2] = Q1
B1 = Split(Q1, "")
For j = 1 To UBound(B1)
B2 = Split(B1(j), "")
B3 = Split(Replace(B2(1), "", ""), ",")
A2(A + 1, 1) = Replace(B2(2), "", "")
A2(A + 1, 2) = Replace(B2(0), "", "")
For K = 0 To 9
A2(A + 1, 3 + K) = B3(K)
Next
A = A + 1
Next
Application.StatusBar = i
Next
MsgBox A
With Sheet1
If .AutoFilterMode = True Then .AutoFilterMode = False
.Rows("2:600000").ClearContents
If A > 0 Then .[A2].Resize(A, 15) = A2
.Rows(1).AutoFilter   '数据筛选
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
End With
End Sub

Excel vba如何抓取指定的网页数据到单元格

2. Excel 求教大神,VBA提取网站数据

正好我有一个这个程序,给你参考一下:
Sub 网页抓取()
Dim i
For i = 2010 To 2014
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.chinabond.com.cn/jsp/include/EJB/document.jsp?sId=0102&sBbly=" & i & "06&sMimeType=4", _
Destination:=Cells(1, i))
.Name = "sheet001"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Next
End Sub

3. EXCEL VBA获取某个需要登录的网站上的数据

可以通过WebBrowser控件的使用实现该功能
以下实例打开百度,在输入框输入“aaa”
Public Sub useie()
'引用Microsoft Internet Controls
Dim IE
On Error Resume Next
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True

IE.Navigate URL:=""
timeie = DateAdd("s", 20, Now()) '等待20s
Do While IE.Busy And Not IE.ReadyState = READYSTATE_COMPLETE
DoEvents
If timeie < Now() Then
MsgBox “无法连接重新执行”
IE.Quit
Exit Sub
End If
Loop

IE.Document.getElementById("kw").Value = "aaa"
Set IE = Nothing
Set ID = Nothing
End Sub

WebBrowser控件的使用
0、常用方法
Navigate(string urlString):浏览urlString表示的网址
Navigate(System.Uri url):浏览url表示的网址
Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息
//(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)
GoBack():后退
GoForward():前进
Refresh():刷新
Stop():停止
GoHome():浏览主页
WebBrowser控件的常用属性:
Document:获取当前正在浏览的文档
DocumentTitle:获取当前正在浏览的网页标题
StatusText:获取当前状态栏的文本
Url:获取当前正在浏览的网址的Uri
ReadyState:获取浏览的状态
WebBrowser控件的常用事件:
DocumentTitleChanged,
CanGoBackChanged,
CanGoForwardChanged,
DocumentTitleChanged,
ProgressChanged,
ProgressChanged

1、获取非input控件的值:
webBrowser1.Document.All["控件ID"].InnerText;
或webBrowser1.Document.GetElementById("控件ID").InnerText;
或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");

2、获取input控件的值:
webBrowser1.Document.All["控件ID"].GetAttribute("value");;
或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");

3、给输入框赋值:
//输入框
user.InnerText = "myname";
password.InnerText = "123456";
webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");

4、下拉、复选、多选:

//下拉框:
secret.SetAttribute("value", "question1"); 
//复选框
rememberme.SetAttribute("Checked", "True");
//多选框
cookietime.SetAttribute("checked", "checked");

5、根据已知有ID的元素操作没有ID的元素:
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;

根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。

6、获取Div或其他元素的样式:
webBrowser1.Document.GetElementById("addDiv").Style;

7、直接执行页面中的脚本函数,带动态参数或不带参数都行:
Object[] objArray = new Object[1];
objArray[0] = (Object)this.labFlightNumber.Text;
webBrowser1.Document.InvokeScript("ticketbook", objArray);
webBrowser1.Document.InvokeScript("return false");

8、自动点击、自动提交:
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
btnAdd.InvokeMember("Click");

9、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:

this.timer1.Enabled = true;
this.timer1.Interval = 1000 * 2;
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Enabled = false;
ClickBtn.InvokeMember("Click");//执行按扭操作
}

10、屏蔽脚本错误:
将WebBrowser控件ScriptErrorsSuppressed设置为True即可

11、自动点击弹出提示框:

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//自动点击弹出确认或弹出提示
IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
}

WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//自动点击弹出确认或弹出提示
IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
//下面是你的执行操作代码
}

12、获取网页中的Iframe,并设置Iframe的src
HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document; 
或
HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document; 
docFrame.All["mainFrame"].SetAttribute("src", "");

13、网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。

14、让控件聚焦
this.webBrowser1.Select();
this.webBrowser1.Focus();
doc.All["TPL_password_1"].Focus();

15、打开本地网页文件
webBrowser1.Navigate(Application.StartupPath + @"\Test.html");

16、获取元素、表单

//根据Name获取元素
public HtmlElement GetElement_Name(WebBrowser wb,string Name)
{
HtmlElement e = wb.Document.All[Name];
return e;
}

//根据Id获取元素
public HtmlElement GetElement_Id(WebBrowser wb, string id)
{
HtmlElement e = wb.Document.GetElementById(id);
return e;
}

//根据Index获取元素
public HtmlElement GetElement_Index(WebBrowser wb,int index)
{
HtmlElement e = wb.Document.All[index];
return e;
}

//获取form表单名name,返回表单
public HtmlElement GetElement_Form(WebBrowser wb,string form_name)
{
HtmlElement e = wb.Document.Forms[form_name];
return e;
}

//设置元素value属性的值
public void Write_value(HtmlElement e,string value)
{
e.SetAttribute("value", value);
}

//执行元素的方法,如:click,submit(需Form表单名)等
public void Btn_click(HtmlElement e,string s)
{

e.InvokeMember(s);
}

EXCEL VBA获取某个需要登录的网站上的数据

4. excel vba 抓取网页数据

这个的话
你必须要对excel里面的VBA的很熟悉啊
你可以去excel home里面询问一些VBA高手啊
这里很难遇到啊

5. 怎么实现EXCEL自动提取网站中的数据

数据菜单获取外部数据
自网站
输入网站的地址
导入
属性设置刷新时间



怎么实现EXCEL自动提取网站中的数据

6. 做一个电子表格excel vba 实时获取网页数据

首先打开Excel 2007工作表,点击想要把数据导入的位置,然后在菜单栏找到第五项"数据“,点击,在“获取外部数据”项下找到“自网站”并点击。

这时弹出来”新建 Web 查询“对话框,可以在“地址”一栏键入要添加数据的网页的地址。

添加完成后,点击“转到(G)”,下面会出来要添加网页的内容和黄色箭头,单击要选择的表旁边的黄色箭头,可以看到点击后黄色箭头变成绿色的对号,这表示内容已经选中了,然后单击“导入(I)”。

弹出来”导入数据“对话框,设置完成点击“确定”。

Excel表中显示“正在获取数据……”。

数据获取完成,之前选中的网页内容全部导入了Excel工作表里。


7. excel VBA 网页数据抓取

首先打开Excel 2007工作表,点击想要把数据导入的位置,然后在菜单栏找到第五项"数据“,点击,在“获取外部数据”项下找到“自网站”并点击。

这时弹出来”新建 Web 查询“对话框,可以在“地址”一栏键入要添加数据的网页的地址。

添加完成后,点击“转到(G)”,下面会出来要添加网页的内容和黄色箭头,单击要选择的表旁边的黄色箭头,可以看到点击后黄色箭头变成绿色的对号,这表示内容已经选中了,然后单击“导入(I)”。

弹出来”导入数据“对话框,设置完成点击“确定”。

Excel表中显示“正在获取数据……”。

数据获取完成,之前选中的网页内容全部导入了Excel工作表里。

excel VBA 网页数据抓取

8. 如何通过Excel的VBA自动提取下面网页上多页数据?

直接复制就行了。
这个用VBA解决不了,只能用按键精灵,可以实现。
最新文章
热门文章
推荐阅读