1、vb获得网页源代码(VB get web source code)VB analyzes web content over 64K (based on XMLHTTP and byte array processing)*Name. WEB Page Read ProgramFile WEBRead.frmVersion. 1.0.0Dependencies XMLHTTP ObjectDescription. Dynamic read URL HTML dataAuthor. Zhou Wen XingDate, Nov, 5nd 2010CSDN AccountsSupermanKingC
2、opyright (c) 2008 by LiuZhou City, China*=API function defining (API function definition)=Private Declare Sub CopyMemory Lib “Kernel32“ Alias “RtlMoveMemory“ (_Destination As Any, _Source As Any, _ByVal Length As Long)=Form event dispose process (basic event handling of windows)=Click the button 1=
3、generated event =Private Sub Command1_Click ()= variable definition =Dim strTemp As String temporary string variablesDim strUserList As String final variable for merging user listsDim strSearch As String search for key content string variablesDim lngSearchSize As Long search for key content string s
4、izeDim lngStart As Long the variable that stores the start position when searching for user stringsDim lngEnd As Long the variable that stores the end of the store when searching for user stringsDim ComXMLHTTP As Object XMLHTTP object accessing web pagesDim byteHTML () As Byte byte stream array vari
5、ables for storing web contentOn Error Resume Next starts setting up error traps to prevent program errors from crashing= initialize variables =StrUserList =“strsearch =“class=”下拉菜单“onmouseover =”菜单(这个ID) ”“lngsearchsize = lenb(StrConv(strsearch,vbfromunicode) )“=开始下载指定 URL的数据内容=集 comxmlhttp = Create
6、Object(“微软。XMLHTTP” )的初始化XMLHTTP对象如果错误,数字0,然后本错误:“错误。数” 、 “错误的描述。呃。清晰退出子最后如果comxmlhttp。打开“获取” 、 “HTTP:/ /论坛。多玩。COM / thread-17408898-2-1 .html”,假“设置访问方式和 URL地址comxmlhttp.setrequestheader”content-type”、 “application/x-www-form-urlencoded”向 HTTP头加入参数comxmlhttp发送的 HTTP请求提交。如果错误,数字0,然后本错误:“错误。数” 、 “错误的描
7、述。呃。清晰退出子最后如果“-判断下载是否成功-如果 comxmlhttp现状 = 0lngstart = lngstart + lngsearchsize“-开始循环查找所有用户内容-做“这里开始才是要找的东西位置lngstart = instr_array(lngstart,bytehtml,strsearch)如果 lngstart = 0lngstart = lngstart + lngsearchsizelngend = instr_array(lngstart,bytehtml, ”)strtemp = mid_array(bytehtml,lngstart,lngend - ln
8、gstart)lngstart = lngendstruserlist = struserlist和 strtemp和 vbcrlf最后如果而 lngstart = 0环最后如果“=完成工作将用户信息合并内容输出到文本框=Text文本= struserlist。端子“=在类的自定义功能用户处理过程(自定义函数及处理过程)“=“-功能名称:utf8tounicode输入参数:funutf8(字节数组)的 UTF-8编码的字节数组返回值:(字节数组)-返回 Unicode字节数组描述:Visual Basic 编译转换 UTF-8 Unicode处理过程作者:supermanking“-功能 ut
9、f8tounicode(ByRef funutf8()字节)作为 byte()“=变量定义=昏暗的 lnglength长昏暗的 lnglengthb长昏暗的 lngutf8char长昏暗的 intwchar为整数昏暗的 bytetemp字节昏暗的 bytebit字节昏暗的 byteunicode()字节昏暗的 lngutf8count长暗淡我一样长暗 j长错误恢复的开始设置错误陷阱,防止程序发生意外错误而崩溃“=初始化变量=lnglengthb = 0“=校验输入参数=lnglength = UBound(funutf8)+ 1如果错误,数字0,然后呃。清晰退出功能最后如果“=开始循环处理编码
10、转换过程=我 lnglength = 0 - 1“-根据 UTF-8编码规则数 UTF-8字符的存储个数-lngutf8count = 0Bytetemp = funutf8 (i)For J = 1 to 7Bytebit = Int (bytetemp / (2 8 j) 二进制位向右偏移 (8) - (j) 个二进制位Bytebit bytebit and 取最后一个二进制位值 = 1 If bytebit = 1 thenLngutf8count lngutf8count = + 1Else碰到 0就结束数字符数操作Exit forEnd ifNext J判断编码内存储的内容是否是经过
11、编码的 - -If lngutf8count or lngutf8count 3 then没有经过 - 格式编码 Unicode utf-8, 直接转换成 编码 -If lnglengthb = 0 thenLnglengthb = 2Byteunicode ReDim (lnglengthb - 1)ElseLnglengthb = lnglengthb + 2ReDim preserve byteunicode (lnglengthb - 1)End ifByteunicode (lnglengthb - 2) = bytetempElse经过 - 格式编码 Unicode utf-8,
12、先读出内容后再转换成 编码 -读出这几个 utf - 8字节内容For J = 0 to lngutf8count - 1Funutf8 bytetemp = (I + j)If J = 0 then第一个 utf - 8编码含编码字节信息, 所以取存储信息特别点Bytetemp and bytetemp = (2 8 - (lngutf8count + 1) - 1)Lngutf8char = bytetempElse后面的只取 6个二进制位Bytetemp = bytetemp & h3fLngutf8char = lngutf8char * H40 & 向左偏移 6位好存储后面的6位数据
13、Lngutf8char = lngutf8char or bytetemp 将低 6位的数据补充到编码中End ifNext J已经取出 unicode编码到 lngutf8char 里If lnglengthb = 0 thenLnglengthb = 2Byteunicode ReDim (lnglengthb - 1)ElseLnglengthb = lnglengthb + 2ReDim preserve byteunicode (lnglengthb - 1)End ifByteunicode (lnglengthb - 2) = lngutf8char and 255Byteuni
14、code (lnglengthb - 1) = Int (lngutf8char / (2 8) and 255)I = I + (- 1 lngutf8count)End ifIf I (lnglength - 1) thenExit forEnd ifNext i= = = = = = = = = = = = = = = = = = = = 完成编码转换过程, 返回数据 = = = = = = = = = = = = = = = = = = = =utf8tounicode = byteunicodeend functionis -is the function name: unicode
15、togb2312input: fununicode (byte array) in the unicodes byte arrays return value (byte array) - return gb2312s byte arraydescription: visual basic compiles conversion the unicode to gb2312 disposed processauthor: supermankingsis -function unicodetogb2312 (byref fununicode () as byte) as byte ()= = =
16、= = = = = = = = = = = = = = = = = 变量定义 = = = = = = = = = = = = = = = = = = = =lnglength as long dimlnglengthb as long dimdim bytegb2312 () as bytedim i as longintwchar as integer dimintchar as integer dimon error resume next is 开始设置错误陷阱, 防止程序发生意外错误而崩溃= = = = = = = = = = = = = = = = = = = = 初始化变量 = =
17、 = = = = = = = = = = = = = = = = = =lnglengthb = 0= = = = = = = = = = = = = = = = = = = = 校验输入参数 = = = = = = = = = = = = = = = = = = = =lnglength = ubound (fununicode) + 1if err. number 0 thenerr. clearexit functionend iflnglength = lnglength / 2= = = = = = = = = = = = = = = = = = = = 开始循环处理编码转换过程 =
18、 = = = = = = = = = = = = = = = = = = =for i = 0 to lnglength - 1copymemory intwchar, fununicode (* 2), 2intchar = asc (strconv (chrw (intwchar), vbnarrow)if intchar intchar 0 thenerr. clearexit functionend ifbytefindarray = strconv (funfind, vbfromunicode)lngfindcount = ubound (bytefindarray)is 校验搜索
19、位置参数 - -if funstart + lngfindcount lngbytescount thenexit functionend if= = = = = = = = = = = = = = = = = = = = 开始搜索数据 = = = = = = = = = = = = = = = = = = = =for i = funstart to lngbytescountlngisfind = 1for j = 0 to lngfindcountif funbytes (i + j) ucase (chr (bytefindarray (j) thenlngisfind = 0退出最后
20、如果其他的如果 funbytes(i+j) lngbytescount然后退出功能最后如果“=开始取指定数据内容=ReDim byteRead(funcount - 1)byteread CopyMemory(0) ,funbytes(funstart) ,funcountmid_array = StrConv(byteread,vbunicode)端功能“-功能名称:savetextfile输入参数:funfilename(字符串)-保存文件的路径“:funbytes(字节数组)保存文件的数据“:funmode(字符串)数据的编码方式返回值:(空)描述:保存 txt文件的处理过程。作者:su
21、permanking“-Sub SaveTextFile(ByVal funfilename 为字符串,_我 funbytes()字节,_选择 ByVal funMode是字符串“GB2312” )“=变量定义=作为整数的弱 fs错误恢复的开始设置错误陷阱,防止程序发生意外错误而崩溃“=校验输入参数=“判断给定文件地址是否可读写,同时也可进行文件数据初始化操作FS = FreeFile打开输出 funfilename作为# FS如果错误,数字0,然后本错误:“错误。数” 、 “错误。描述,16, “错误”呃。清晰退出子最后如果近# FS“=开始写文件数据=FS = FreeFile二开 fun
22、filename作为# FS“根据编码模式来写 txt文件头,这样可让 Windows记事本识别该文件的编码方式选择案例 ucase(funmode)“GB2312”:“输出 GB2312编码的文本文件把# 1, 1,funbytes“unicode”:“输出 Unicode编码的文本文件把# 1, 1,cbyte(与 HFF)把# 1, 2,cbyte(和 HFE)把# 1, 3,funbytes案例“UTF-8”:“输出 UTF-8编码的文本文件把# 1, 1,cbyte(与人)把# 1, 2,cbyte(和 HBB)把# 1, 3,cbyte(和 HBF)把# 1, 4,funbytes最后选择近# FS端子