数据库学习:用Access分析网站实例
问题:
如何用 Access 分析一个网站,或者在网站上提交一个查询,得到结果后存储进哪?
如何用 Access 下载 DVBBS 论坛上所有的帖子?
如何用 ACCESS 编写灌水程序?
方法一:
答案非常简单—— DHTML 编程。
有人可能问了,ACCESS 使用的是 VBA ,而 DHTML 中使用的是 VBS 怎么可能通用哪?其实 VBS / VBA 都是 VB 的子集。在 Access 中只要引用
Microsoft Internet Controls
Microsoft HTML Object Library
即可,然后在窗体上加入 “Microsoft Web 浏览器”控件
好了,下面就以我写的一个读取某 IP 物理地域查询网站页面的为例说明 DHTML / “Microsoft Web 浏览器”控件在 ACCESS 的应用吧。
“Microsoft Web 浏览器”控件的作用是什么哪?主要就是为了获得 DOCUMENT 对象的,DHTML 的操作都是以 DOCUMENT 对象为运行基础的。
要完成读取网页的功能我们必须了解 DHTML 的几个简单的对象以及属性
1、DOCUMENT 对象:代表这整个 HTML 文档
2、BODY 对象:是 DOCUMENT 对象的子对象,里面存储着所有显示给用户看的 HTML 代码
3、innerText 属性:页面中显示给客户看的文本,注意:不是 HTML 代码呦
4、innerHTML属性:构成页面的 HTML 代码
5、对象.all.length属性:页面中所有 element 的个数。(all 用于表示所有对象)
Option Compare Database
Dim blnSwitch As Boolean
Private Sub Command1_Click() '用于启动浏览功能
Me.WebBrowser3.Navigate ("http://ip.loveroot.com/index.php?job=search")
End Sub
Private Sub Command11_Click() '将需要搜索的IP 写入全局变量
splitIP Text1.Value
End Sub
Function splitIP(strip) '将需要搜索的IP 写入全局变量
Dim a() As String
strip = strip & "."
a = Split(strip, ".")
Dim i As Long
For i = 0 To UBound(a)
If a(i) = "" Then a(i) = "0"
lngSearchIP(4 - i) = CLng(a(i))
Next i
End Function
Sub WriteLog(ip1 As String) '读取结果
Dim dc As MSHTML.HTMLDocument
Dim Bd As MSHTML.HTMLBody
Dim El As MSHTML.HTMLElementCollection
Dim strip As String
Dim strAdd As String
Dim strSql
Dim i As Long
Set dc = WebBrowser3.Document
Set Bd = dc.body
Dim lngStart As Long
'循环 DOCUMENT 中所有的元素获取需要的字符
For i = 0 To dc.all.length - 1
'由于该服务器重写界面,我改了一下分析代码
'If dc.all(i).tagName = "p" And Left(dc.all(i).innerText, 4) = "查询结果" Then
If dc.all(i).tagName = "p" And Left(dc.all(i).innerText, 8) = "官方数据查询结果" Then
'由于该服务器重写界面,我改了一下分析代码
'strAdd = Mid(dc.all(i).innerText, InStr(1, dc.all(i).innerText, "(") + 2, InStr(1, dc.all(i).innerText, ")") - InStr(1, dc.all(i).innerText, "(") - 3)
'strip = Mid(dc.all(i).innerText, InStr(1, dc.all(i).innerText, "查询结果:") + 6, InStr(1, dc.all(i).innerText, "(") - InStr(1, dc.all(i).innerText, "查询结果:") - 7)
strAdd = Right(dc.all(i).innerText, Len(dc.all(i).innerText) - InStr(dc.all(i).innerText, " - ") - 3)
strip = strNowIP
LabelSIP.Caption = strip & strAdd
'ok 终于得到需要的数据了,用 SQL 语句直接写入吧
strSql = "update ipaddress set [ip1]='" & strip & "',[add]='" & strAdd & "' where mark='last'"
CurrentProject.Connection.Execute strSql
strSql = "insert into ipaddress([ip1],[add],[mark],[enip]) values('" & strip & "','" & strAdd & "','no'," & CStr(enaddr(strip)) & ")"
CurrentProject.Connection.Execute strSql
Exit For
End If
Next i
Dim strNewIP As String
strNewIP = refreshIP
On Error Resume Next
'利用 DHTML 的 innerHTML 来更改网页的源代码,建立一个简单的 FORM ,然后提交给服务器,继续查询下面的 IP
Bd.innerHTML = "<form method='POST' action='index.php?job=search' target='_parent'><input type='text' name='search_ip' ><input type='submit' value='查询' name='B1'></form>"
'在 INPUT TEXT search_ip 中填入 IP。
dc.all.Item("search_ip").Value = strNewIP
'用 DHTML 提交 FORM 到服务器
dc.all.Item("b1").Click
End Sub
Private Sub Form_Open(Cancel As Integer)
Text1.Value = Nz(DLookup("ip1", "ipaddress", "[mark]='last" & Me.Caption & "'"), "1.0.0.0")
End Sub
Private Sub WebBrowser3_DownloadComplete()
'该事件在页面成功下载到本地时运行,这时候 DOCUMENT 对象
'已经完全被客户端浏览器读取了,我们只要获取 Body 对象中的 innerHTML 即可
If Len(strNowIP) = 0 Then
splitIP Text1.Value
End If
If check1.Value = True Then
Call WriteLog("61.12.15.117")
End If
End Sub
Dim i As Long
lngSearchIP(2) = lngSearchIP(2) + 1
For i = 2 To 4
If lngSearchIP(i) >= 256 Then
lngSearchIP(i) = 0
lngSearchIP(i + 1) = lngSearchIP(i + 1) + 1
End If
Next i
refreshIP = Format(lngSearchIP(4), "0") & "." & Format(lngSearchIP(3), "0") & "." & Format(lngSearchIP(2), "0") & "." & Format(lngSearchIP(1), "0")
strNowIP = refreshIP
Debug.Print refreshIP
End Function
以下代码请新建一个模块后 COPY 进去
Option Compare Database
Public lngSearchIP(4) As Long
Public strNowIP As String
Public strOKAddress As String
Public strOKIP As String
Public blnStop As Boolean
Function writeOKIP()
Dim rs As New ADODB.Recordset
Dim strSql As String
strSql = "select * from ipaddress order by enip"
rs.Open strSql, CurrentProject.Connection, 1, 1
Dim strAdd1 As String
Dim strIP1 As String
Dim lngENIP1 As Long
Dim strState As String
strState = "start"
Dim
