找回密码
 加入
搜索
查看: 8071|回复: 18

[IE类操作] [已解决]正则表达式提取网页源码数据

 火.. [复制链接]
发表于 2010-4-23 18:27:50 | 显示全部楼层 |阅读模式
本帖最后由 acbs 于 2010-4-24 14:36 编辑

这是一个根据手机号码查询手机号归属地的小代码,其中使用正则表达式提取返回的数据不太会,请各位指点,新手刚学,请大家帮忙,谢谢。

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("手机号归属地查询工具", 270, 270, 200, 120)
$Label1 = GUICtrlCreateLabel("手机号码", 20, 20, 80, 20)
$Input1 = GUICtrlCreateInput("在此输入手机号", 90, 18, 170, 20)
$Button1 = GUICtrlCreateButton("确定查询", 10, 50, 250, 40, 0)
$Label2 = GUICtrlCreateLabel("归属地:", 10, 112, 84, 28)
$Label3 = GUICtrlCreateLabel("卡类型:", 10, 151, 74, 28)
$Label4 = GUICtrlCreateLabel("区 号:", 10, 192, 64, 28)
$Label5 = GUICtrlCreateLabel("邮 编:", 10, 227, 64, 28)
$Input2 = GUICtrlCreateInput("", 90, 105, 169, 32)
$Input3 = GUICtrlCreateInput("", 90, 147, 169, 32)
$Input4 = GUICtrlCreateInput("", 90, 190, 169, 32)
$Input5 = GUICtrlCreateInput("", 90, 227, 169, 32)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

        EndSwitch
WEnd
#include <IE.au3> ;下面的链接可以查电话号码归属地
$ie = InputBox("查询电话","请输入电话号码:","1")
$str="http://www.ip138.com:8080/search.asp?action=mobile&mobile="&$ie
$OIE =_IECreate ($str)

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2010-4-23 20:06:07 | 显示全部楼层
IP138页面臃肿,影响速度
不如用http://api.showji.com/locating/?m=13903111234&output=text
提供的接口速度块多了,使用也方便

评分

参与人数 1金钱 +15 收起 理由
afan + 15

查看全部评分

 楼主| 发表于 2010-4-23 22:10:37 | 显示全部楼层
关键不是用哪个网站,而是提取网页源码中的数据所用的正则不会。
发表于 2010-4-23 22:32:13 | 显示全部楼层
参考一下吧。。
我试一下还是可以用的
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("手机号归属地查询工具", 270, 270, 200, 120)
$Label1 = GUICtrlCreateLabel("手机号码", 20, 20, 80, 20)
$Input1 = GUICtrlCreateInput("在这里输入手机号", 90, 18, 170, 20,$ES_NUMBER)
$Button1 = GUICtrlCreateButton("确定查询", 10, 50, 250, 40, 0)
$Label2 = GUICtrlCreateLabel("归属地:", 10, 112, 84, 28)
$Label3 = GUICtrlCreateLabel("卡类型:", 10, 151, 74, 28)
$Label4 = GUICtrlCreateLabel("区 号:", 10, 192, 64, 28)
$Label5 = GUICtrlCreateLabel("邮 编:", 10, 227, 64, 28)
$Input2 = GUICtrlCreateInput("", 90, 105, 169, 32)
$Input3 = GUICtrlCreateInput("", 90, 147, 169, 32)
$Input4 = GUICtrlCreateInput("", 90, 190, 169, 32)
$Input5 = GUICtrlCreateInput("", 90, 227, 169, 32)
GUISetState(@SW_SHOW)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $telno = GUICtrlRead($Input1)
                        GUICtrlSetState($Button1,$gui_disable)
                        $telinfo = find138($telno)
                        GUICtrlSetState($Button1,$gui_enable)
                        If $telinfo[0] Then
                                GUICtrlSetData($Input2,$telinfo[2])
                                GUICtrlSetData($Input3,$telinfo[3])
                                GUICtrlSetData($Input4,$telinfo[4])
                                GUICtrlSetData($Input5,$telinfo[5])
                        Else
                                Exit
                        EndIf
        EndSwitch
WEnd

Func find138($phn)        
        Local $telinfo[6] = [0, 0, 0, 0, 0,0], $dt = 0
        $str = InetGet("http://www.ip138.com:8080/search.asp?action=mobile&mobile=" & $phn,@TempDir & "\data.tmp", 1, 1)
        Do
                Sleep(250)
                $dt += 1
                If $dt > 250 Then Return SetError(1,0,0)
        Until InetGetInfo($str, 2)
        InetClose($str)
        $str = FileRead(@TempDir & "\data.tmp")
        FileDelete(@TempDir & "\data.tmp")
        $phoNo = StringRegExp($str, '您查询的手机号码段</TD>(?s)(.[^>]*)>(.[^<]*)(.*)' & _
                        '卡号归属地</TD>(?s)(.[^>]*)>(.[^<]*)(.*)' & _
                        '卡 类 型</TD>(?s)(.[^>]*)>(.[^<]*)(.*)' & _
                        '区 号</TD>(?s)(.[^>]*)>(.[^<]*)(.*)' & _
                        '邮 编</TD>(?s)(.[^>]*)>(.[^<]*)', 3)
        If Not @error Then
                $telinfo[1] = $phoNo[1]
                $telinfo[2] = StringReplace($phoNo[4]," "," ")
                $telinfo[3] = $phoNo[7]
                $telinfo[4] = $phoNo[10]
                $telinfo[5] = $phoNo[13]
                $telinfo[0] = 5
        Else
                MsgBox(0, "  出错!!", "查询错误!!" & @CRLF & "错误代码: " & @error)
        EndIf
        Return $telinfo
EndFunc   ;==>find138

评分

参与人数 2金钱 +40 收起 理由
acbs + 10 谢谢,辛苦了
水木子 + 30 不错!学习啦!

查看全部评分

 楼主| 发表于 2010-4-24 08:40:09 | 显示全部楼层
谢谢楼上的,学习消化一下。
 楼主| 发表于 2010-4-24 09:21:56 | 显示全部楼层
提示出错了,不知道啥原因:
ERROR: InetGetInfo(): undefined function.
        Until InetGetInfo($str, 2)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
发表于 2010-4-24 10:01:08 | 显示全部楼层
测试了6楼的代码,不错能够正常使用。
发表于 2010-4-24 10:02:55 | 显示全部楼层
回复 8# acbs

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2010-4-24 13:33:24 | 显示全部楼层
那为什么我的会有这个错误提示,楼上的可以指点一下吗?
发表于 2010-4-24 13:45:09 | 显示全部楼层
回复 11# acbs


    可能你的autoit的程序问题。。。你重au3装一下
像是少文件。。
或是版本太老
下个新的试 试
发表于 2010-4-24 14:31:11 | 显示全部楼层
#include <Inet.au3>
Local $Text[4] = ['号码归属地:', '卡  类  型:', '区      号:', '邮      编:'], $sText, $z = 0

$Number = InputBox('手机号码归属地查询', '请输入需要查询的手机号!')

If @error = 0 Then
        $Source = _INetGetSource('http://www.ip138.com:8080/search.asp?action=mobile&mobile=' & $Number)
        $sRep = StringRegExpReplace($Source, ' |\n|\r|\s', '')
        $sReg = StringRegExp($sRep, '卡号归属地</TD>.+?tdc2>(.+?)</TD>|卡类型.+?tdc2>(.+?)</TD>|区号</TD>.+?tdc2>(.+?)</TD>|邮编</TD>.+?tdc2>(.+?)<ahref', 3)
        For $i = 0 To UBound($sReg) -1
                If $sReg[$i] = '' Then ContinueLoop
                $sText &= $Text[$z] & $sReg[$i] & @CRLF & @CRLF
                $z += 1
        Next
        MsgBox(0, '查询结果:', $sText)
EndIf

评分

参与人数 1金钱 +20 收起 理由
acbs + 20 谢谢,学习了

查看全部评分

 楼主| 发表于 2010-4-24 14:35:06 | 显示全部楼层
13楼不错,谢啦,运行通过。
消化一下。
发表于 2010-4-24 21:51:27 | 显示全部楼层
又学习了一些
发表于 2010-4-30 15:04:41 | 显示全部楼层
从此程序中学习了大量的知识,不错呀!感谢楼主的提问和感谢同胞们无私的觖答!
发表于 2010-5-10 11:11:36 | 显示全部楼层
学习ing,谢谢
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-12 10:51 , Processed in 0.103712 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表