怎么用VBA或网络爬虫程序抓取网站数据

怎么用VBA或网络爬虫程序抓取网站数据,第1张

VBA网抓常用方法

1、xmlhttp/winhttp法:

用xmlhttp/winhttp模拟向服务器发送请求,接收服务器返回的数据

优点:效率高,基本无兼容性问题。

缺点:需要借助如fiddler的工具来模拟http请求。

2、IE/webbrowser法:

创建IE控件或webbrowser控件,结合htmlfile对象的方法和属性,模拟浏览器 *** 作,获取浏览器页面的数据。

优点:这个方法可以模拟大部分的浏览器 *** 作。所见即所得,浏览器能看到的数据就能用代码获取。

缺点:各种d窗相当烦人,兼容性也确实是个很伤脑筋的问题。上传文件在IE里根本无法实现。

3、QueryTables法:

因为它是excel自带,所以勉强也算是一种方法。其实此法和xmlhttp类似,也是GET或POST方式发送请求,然后得到服务器的response返回到单元格内。

优点:excel自带,可以通过录制宏得到代码,处理table很方便

。代码简短,适合快速获取一些存在于源代码的table里的数据。

缺点:无法模拟referer等发包头

也可以利用采集工具进行采集网页端的数据,无需写代码。

数据库可以下载,但是要看是什么数据库,比如MDB为后缀的数据库就能下载,当然现在网站空间服务器都有防mdb没下载的功能,但是SQL的数据库你也只有在网站上面用phpadmin这个软件来进行数据库备份,然后下载数据库(那个软件我好像忘了是不是叫这个),其实也有很多的工具,如SQL TOOLS这个软件也可以!只不过是英文的,但也有汉化的,刚开始可能不是很好用!但是这个两个都需要有SQL的数据库帐号和密码。并且是管理员帐号密码!事情没有那么绝对!

1,先表明一下你的说法,“从浏览器中复制表格到一个文本文档或剪贴板”

表格复制后放入文体文档是只能保留文字和数据,这种格式为TXT文档,站点有提供下载功能,

另站点还提供XLS格式的EXCEL表格下载。

如果你只需要开奖数据,以上已经足够。

2,做VBA动态更新分析表所用到的。

经过查看你提供的网页源代码,复制了关于数据这一段:

<div class="kai_tab" id="fast_issue_content">

<table class="kuai_tab fucai_tab" id="fastIssueList">

<thead>

<tr>

<td class="kuai_w1">期数</td>

<td class="kuai_w2">开奖号码</td>

<td>开奖时间</td>

<td>中奖组合</td>

<td></td>

</tr>

</thead>

<tbody>

</tbody>

</table>

</div>

</div>

</div>

并查了class和ID属性值,所引用的是经过JavaScript处理调用数据库的值,不是一个文本文件的集合,

如:双色球的文体文件如下:http://www.17500.cn/getData/ssq.TXT

这种才可好处理,示列如下:

Private Sub CommandButton1_Click()

Range("A3:AC3500").Clear

k3dshijihao = "http://www.17500.cn/getData/ssq.TXT"

d3s = "WData3D_All"

Cells(2, 1) = "开奖期号"

Cells(2, 2) = "开奖日期"

Cells(2, 3) = "红"

Cells(2, 4) = "球"

Cells(2, 5) = "大 "

Cells(2, 6) = "小"

Cells(2, 7) = "顺"

Cells(2, 8) = "序"

Cells(2, 9) = "蓝"

Cells(2, 10) = "红"

Cells(2, 11) = "球"

Cells(2, 12) = "出"

Cells(2, 13) = "球"

Cells(2, 14) = "顺"

Cells(2, 15) = "序"

Cells(2, 16) = "投注总额"

Cells(2, 17) = "奖池金额"

Cells(2, 18) = "一等注数"

Cells(2, 19) = "一等金额"

Cells(2, 20) = "二等注数"

Cells(2, 21) = "二等金额"

Cells(2, 22) = "三等注数"

Cells(2, 23) = "金额"

Cells(2, 24) = "四等注数"

Cells(2, 25) = "金额"

Cells(2, 26) = "五等注数"

Cells(2, 27) = "金额"

Cells(2, 28) = "六等注数"

Cells(2, 29) = "金额"

cz = k3dshijihao: czmc = d3s

With ActiveSheet.QueryTables.Add(Connection:= _

"TEXT" &cz, Destination:=Range("A3"))

.Name = czmc

.FieldNames = True

.RowNumbers = False

.FillAdjacentFormulas = False

.PreserveFormatting = True

.RefreshOnFileOpen = False

.RefreshStyle = xlInsertDeleteCells

.SavePassword = False

.SaveData = True

.AdjustColumnWidth = True

.RefreshPeriod = 0

.TextFilePromptOnRefresh = False

.TextFilePlatform = xlWindows

.TextFileStartRow = 1

.TextFileParseType = xlDelimited

.TextFileTextQualifier = xlTextQualifierDoubleQuote

.TextFileConsecutiveDelimiter = True

.TextFileTabDelimiter = False

.TextFileSemicolonDelimiter = False

.TextFileCommaDelimiter = False

.TextFileSpaceDelimiter = True

.TextFileColumnDataTypes = Array(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

.TextFileTrailingMinusNumbers = True

.Refresh BackgroundQuery:=False

End With

Range("A" &(Application.Count(Range("a1:a3000")))).Select

End

End Sub

将以上代码贴到VBA中,再加个按钮指定它,试试结果,如果是你想要的这种结果

可以将引用的http://www.17500.cn/getData/ssq.TXT。改为你网站下载来的TXT文件路径,再做相关VBA代修改就可以完成!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10014327.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存