ac5474012 发表于 2009-9-29 09:01:28

求助如何获取一段网页中的文字

我用

$oIE2 = _IECreate (http://www.uname.cn/html/dic/8/12_7868.shtml)
$sText = _IEBodyReadText ($oIE2)
IniWrite("C:\Documents and Settings\Administrator\桌面\a1.ini", "Section3", "key", $sText)

来获取一个网页中的一段代码,就是下面这一段
季 jìgwai3季 子88木
如果从网页内直接复制是这样
季jì gwai3 季子 8 8 木
他们相对的是
汉字 拼音 粤语繁体 部首 姓名学笔画 笔画 五行
我要把这些资料放到excel中,但是我要如何赛选或有没有其他获取的方法?

季 jìgwai3季 子88木;没有空格我不好赛选

顽固不化 发表于 2009-9-29 10:08:39

本帖最后由 顽固不化 于 2009-9-29 10:14 编辑

正学正则。乱编一个:#include <IE.au3>
$oIE2 = _IECreate ("http://www.uname.cn/html/dic/8/12_7868.shtml","",0)
$sText =_IEBodyReadHTML($oIE2)
_IEQuit ( $oIE2 )
$HZ=StringRegExp($sText,"<TD bgColor=#ffffff height=30>(.+)&nbsp;</TD>",3)
$other=StringRegExp($sText,"<TD bgColor=#ffffff>(.+)</TD>",3)
$PY=$other
$YY=$other
$FT=StringLeft($other,1)
$BS=$other
$XB=$other
$BH=$other
$WX=StringLeft($other,1)

$H1="汉字    拼音    粤语    繁体   部首   姓名学笔画笔画五行"&@CRLF
$H2=" "&$HZ&"      "&$PY&"         "&$YY&"    "&$FT&"         "&$BS&"         "&$XB & _
    "               "&$BH&"         "&$WX
MsgBox(0,0,$H1&$H2)

ac5474012 发表于 2009-9-29 18:11:54

为什么叫错误的数组或数组中的文字

afan 发表于 2009-9-29 18:36:06

试下#include <INet.au3>
$url = "http://www.uname.cn/html/dic/8/12_7868.shtml"
$oIE = _INetGetSource($url)
$sR = StringRegExp($oIE, '#FFFFFF">(.*?)(&nbsp;)?</td>', 3)
If @error = 0 Then
        MsgBox(0, '基本资料', _
                        '汉字   :' & $sR & @CRLF & _
                        '拼音   :' & $sR & @CRLF & _
                        '粤语   :' & $sR & @CRLF & _
                        '繁体   :' & $sR & @CRLF & _
                        '部首   :' & $sR & @CRLF & _
                        '姓名学笔画:' & $sR & @CRLF & _
                        '笔画   :' & $sR & @CRLF & _
                        '五行   :' & $sR)
EndIf

ac5474012 发表于 2009-9-29 19:02:27


#include <IE.au3>
#include <INet.au3>

$file = FileOpen("zi.txt", 0)

; 检查打开的文件是否可读
If $file = -1 Then
    MsgBox(0, "错误", "不能打开文件.")
    Exit
EndIf

While 1
    $chars = FileRead($file, 2)
    If @error = -1 Then ExitLoop
;~   MsgBox(0, "读取的字符:", $chars)




$oIE = _IECreate ("http://www.uname.cn/html/dic/4/63_9084.shtml")

$a=_IEGetObjByName($oIE,"Query")

_IEFormElementSetValue ($a, $chars);账号

$oSubmit = _IEGetObjByName ($oIE, "Query")
_IEAction ($oSubmit, "focus")

Sleep(2000)
Send ("{enter}")

_IEQuit ($oIE)
;~ MsgBox(0,"","")
Sleep(3000)
WinWait("在线字典","完毕")
If Not WinActive("在线字典","链接") Then WinActivate("在线字典","完毕")
WinWaitActive("在线字典","完毕")
Sleep(2000)
;$text = WinGetText("在线字典", "")
;~ MsgBox(0, "读取的文本为:","读取到的文本为: " & $var)

$var = ControlGetText("在线字典", "", "Edit1")

WinClose("在线字典", "完毕")
;MsgBox(0,"",$var)
;~ $oIE2 = _IECreate ($var)
;~ $sHTML = _IEDocReadHTML ($oIE2)
;~ $sText = _IEBodyReadText ($oIE2)

;~ IniWrite("C:\Documents and Settings\Administrator\桌面\a1.ini", "Section3", "key", $sHTML)

;~ $oIE2 = _IECreate ($var,"",0)
;~ $sText =_IEBodyReadHTML($oIE2)
;~ _IEQuit ( $oIE2 )
;~ $HZ=StringRegExp($sText,"<TD bgColor=#ffffff height=30>(.+)&nbsp;</TD>",3)
;~ $other=StringRegExp($sText,"<TD bgColor=#ffffff>(.+)</TD>",3)
;~ $PY=$other
;~ $YY=$other
;~ $FT=StringLeft($other,1)
;~ $BS=$other
;~ $XB=$other
;~ $BH=$other
;~ $WX=StringLeft($other,1)



$oIE = _INetGetSource($var)
$sR = StringRegExp($oIE, '#FFFFFF">(.*?)(&nbsp;)?</td>', 3)
If @error = 0 Then
      MsgBox(0, '基本资料', _
                        '汉字   :' & $sR & @CRLF & _
                        '拼音   :' & $sR & @CRLF & _
                        '粤语   :' & $sR & @CRLF & _
                        '繁体   :' & $sR & @CRLF & _
                        '部首   :' & $sR & @CRLF & _
                        '姓名学笔画:' & $sR & @CRLF & _
                        '笔画   :' & $sR & @CRLF & _
                        '五行   :' & $sR)
EndIf


;~ $H1="汉字    拼音    粤语    繁体   部首   姓名学笔画笔画五行"&@CRLF
;~ $H2=" "&$HZ&"      "&$PY&"         "&$YY&"    "&$FT&"         "&$BS&"         "&$XB & _
;~   "               "&$BH&"         "&$WX
;~         MsgBox(0,0,$H1&$H2)
IniWrite("a2.ini", "Section3", $chars, $sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR)
;~ _IEQuit ($oIE2)

;~ $result = StringInStr($sHTML, "<TD bgColor=#ffffff height=30>")
;~ MsgBox(0, "Search result:", $result)
Wend为什么这样不行呢?

afan 发表于 2009-9-29 19:39:03

IniWrite("a2.ini", "Section3", $chars, $sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR)

ac5474012 发表于 2009-9-29 20:44:23

把我那段代码运行一下试试就知道了
#include <IE.au3>
#include <INet.au3>
$file = FileOpen("zi.txt", 0)
If $file = -1 Then
    MsgBox(0, "错误", "在脚本目录下建立zi.txt的文本文件在写几个汉字")
    Exit
EndIf
While 1
    $chars = FileRead($file, 2)
    If @error = -1 Then ExitLoop
$oIE = _IECreate ("http://www.uname.cn/html/dic/4/63_9084.shtml")

$a=_IEGetObjByName($oIE,"Query")

_IEFormElementSetValue ($a, $chars);账号

$oSubmit = _IEGetObjByName ($oIE, "Query")
_IEAction ($oSubmit, "focus")
Sleep(2000)
Send ("{enter}")
_IEQuit ($oIE)
Sleep(3000)
WinWait("在线字典","完毕")
If Not WinActive("在线字典","完毕") Then WinActivate("在线字典","完毕")
WinWaitActive("在线字典","完毕")
Sleep(2000)
$var = ControlGetText("在线字典", "", "Edit1")
WinClose("在线字典", "完毕")
ProcessClose("IEXPLORE.EXE")
$oIE2 = _INetGetSource($var)
$sR = StringRegExp($oIE2, '#FFFFFF">(.*?)(&nbsp;)?</td>', 3)
If @error = 0 Then
;~         MsgBox(0, '基本资料', _
;~                         '汉字   :' & $sR & @CRLF & _
;~                         '拼音   :' & $sR & @CRLF & _
;~                         '粤语   :' & $sR & @CRLF & _
;~                         '繁体   :' & $sR & @CRLF & _
;~                         '部首   :' & $sR & @CRLF & _
;~                         '姓名学笔画:' & $sR & @CRLF & _
;~                         '笔画   :' & $sR & @CRLF & _
;~                         '五行   :' & $sR)
EndIf
IniWrite("a2.ini", "Section3", $chars, $sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR&","&$sR)
Wend

ac5474012 发表于 2009-9-29 20:47:04

有的机器会报 错误的数组或数组中的文字,有的机器就自动退出

试试我这几个字
金镇锁镕镉镑镈镐镓钨镒醪醥医酱覆鸡竅蹜鞦癖皦镏锽锖钻销鍌鍙鏊木槛櫂櫅檺檷櫃槟秾穑穢糧蕡蕇荞蒇蕃蕨荨簥篑箪簧簠簰簦簨褡诺鵚髁杂鬆檭梿梼枱櫂柠檽芜蕧蕙蕊蕉蔫蕞蕈荛蕫报蕤蕃蒴蕺芸鄀简簪馥廓水鸿涛濠濕濡济濢滨澶澀濮濩滥濬鮶鮿鯀鯆懦擩竄鄠鹁潍濲濣澈潲濴涟濜濦瀑濝濞濯泞鯉鯁鯀霟霘霤火鹅爆爃爁膳腻縢膦骈骑骐骗辘转顐撒拧嬣狞鞨鞮鞘缯织缭韪题遧蹧蹠蟫蟟鹄闯阘阊职觞谩懟鄣飏燻烬燿曜曛膨膰膵膮转骓隍谕谓谞靛脾土丰圹擤环璦珰璐璵璩蟥蟠蟢蛲谨蹟翘謦釐闿旛婶壘艟贽觐鄞隗颙崸鹆翻岳甕儱璪璨璯瑯礌礎璧绕嬞儭懥懠搁擬擠擦摈礼袯襆獯踰蹤蹢蟜蹒蝉蟪谬谪讴归瞿睑瞻虫聂阖阗阙关屩癜雏虱杂戳觑貙蹙躯蹩适鬈聩断欤翺鄙鄘陨隘隔颜额颚觞黠噜焘黟魏繠缮缋遨遭遮双叢鞭鞬馊飕丛十八画

ac5474012 发表于 2009-9-29 21:02:30

而且会变成这样
蒗=蒗,làng,long6,蒗,艹,16,13,水
锩=锩,juǎn,錈,&nbsp;,钅,16,13,金
蒤=蒤,tú,,蒤,艹,16,13,火
蔤=蔤,mì,mat6,蔤,艹,17,14,水
蔑=蔑,miè,mit6,蔑,艹,17,14,<a href="/intro.asp">关于我们</a> | <a href="/contact.asp">联系我们</a> | <a href="/navigation.asp">站点地图</a> | <a href="/service.asp">帮助中心</a> | <a href="/service_7.asp">服务条款</a> | <a href="/FriendSite.asp">友情链t接</a> | <a href="/service_6.asp">意见与建议</a> | <a href="/intro_4.asp">诚邀加盟</a> | <a href="/bar/index.asp">逸名吧</a> | <a href="/User/User_StartPay.asp">在线支付</a>
蔒=蔒,hūn,,蔒,艹,17,14,木
懋=懋,mào,mau6,懋,心,17,17,木
箦=箦,zé,zaak3,簀,竹,17,14,木
箭=箭,jiàn,zin3,箭&nbtsp;,15,15,木,<a href="/intro.asp">关于我们</a> | <a href="/contact.asp">联系我们</a> | <a href="/navigation.asp">站点地图</a> | <a href="/service.asp">帮助中心</a> | <a href="/service_7.atsp">服务条款</a> | <a href="/FriendSite.asp">友情链接</a> | <a href="/service_6.asp">意见与建议</a> | <a href="/intro_4.asp">诚邀加盟</a> | <a href="/bar/index.asp">逸名吧</a> | <a href="/User/User_StartPay.asp">在线支付</a>
檗=檗,bò,baak3、paak3,檗&nbspt;,17,17,木,<a href="/intro.asp">关于我们</a> | <a href="/contact.asp">联系我们</a> | <a href="/navigation.asp">站点地图</a> | <a href="/service.asp">帮助中心</a> | <a href="/service_7.asp">t服务条款</a> | <a href="/FriendSite.asp">友情链接</a> | <a href="/service_6.asp">意见与建议</a> | <a href="/intro_4.asp">诚邀加盟</a> | <a href="/bar/index.asp">逸名吧</a> | <a href="/User/User_StartPay.asp">在线支付</a>
泽=泽,zé,zaak6,澤,氵,17,8,水

ac5474012 发表于 2009-9-29 21:11:14

我对比了一下,发现没有什么不同。我不知道为什么它不选择木?请问还有没有什么好办法?
                        <td height="30" bgcolor="#FFFFFF">蔑&nbsp;</td>
                        <td bgcolor="#FFFFFF">miè</td>
                        <td bgcolor="#FFFFFF">mit6</td>
                        <td bgcolor="#FFFFFF">蔑&nbsp;</td>
                        <td bgcolor="#FFFFFF">艹</td>
                        <td bgcolor="#FFFFFF">17</td>
                        <td bgcolor="#FFFFFF">14</td>
                        <td bgcolor="#FFFFFF">木</td>
;上面这个是有问题的
                        <td height="30" bgcolor="#FFFFFF">蔤&nbsp;</td>
                        <td bgcolor="#FFFFFF">mì</td>
                        <td bgcolor="#FFFFFF">mat6</td>
                        <td bgcolor="#FFFFFF">蔤&nbsp;</td>
                        <td bgcolor="#FFFFFF">艹</td>
                        <td bgcolor="#FFFFFF">17</td>
                        <td bgcolor="#FFFFFF">14</td>
                        <td bgcolor="#FFFFFF">水</td>
;这个是没有问题的

ac5474012 发表于 2009-9-29 21:37:02

现在的问题是,单个没有问题。但是一起就不行了。这是不是数组的关系。能不能有什么办法解决?

afan 发表于 2009-9-29 22:08:57

本帖最后由 afan 于 2009-9-29 22:11 编辑

修改了下#include <IE.au3>

$file = FileOpen("zi.txt", 0)
If $file = -1 Then
        MsgBox(0, "错误", "在脚本目录下建立zi.txt的文本文件在写几个汉字")
        Exit
EndIf

While 1
        $chars = FileRead($file, 2)
        If @error = -1 Then ExitLoop
        $oIE = _IECreate("http://www.uname.cn/html/dic/4/63_9084.shtml")
        $a = _IEGetObjByName($oIE, "Query")
        _IEFormElementSetValue($a, $chars);账号
        $oSubmit = _IEGetObjByName($oIE, "Query")
        WinWaitActive("在线字典")
        _IEAction($oSubmit, "focus")
        Send("{enter}")
        _IEQuit($oIE)
        WinWait("在线字典", "完毕")
        If Not WinActive("在线字典", "完毕") Then WinActivate("在线字典", "完毕")
        Sleep(2000)
        WinWaitActive("在线字典", "完毕")
        $var = ControlGetText("在线字典", "", "Edit1")
        WinClose("在线字典", "完毕")
        ProcessClose("IEXPLORE.EXE")
        $oIE2 = _XmlHttp($var)
        $sR = StringRegExp($oIE2, '#FFFFFF">(.*?)(&nbsp;)?</td>', 3)
        If @error = 0 Then
                IniWrite("a2.ini", "Section3", $chars, $sR & "," & $sR & "," & $sR & "," & $sR & "," & $sR & "," & $sR & "," & $sR & "," & $sR)
        EndIf
WEnd

Func _XmlHttp($Url) ;Sanhen
        Local $oHTTP, $sReturn
        $oHTTP = ObjCreate("microsoft.xmlhttp")
        $oHTTP.Open("get", $Url, False)
        $oHTTP.Send()
        $sReturn = BinaryToString($oHTTP.responseBody)
        Return $sReturn
EndFunc   ;==>_XmlHttp还是改用microsoft.xmlhttp靠得住,以下是部分测试的结果,没继续测试

金=金,jīn,gam1,金,金,8,8,金
镇=镇,zhèn,zan3,鎮,钅,18,15,金
锁=锁,suǒ,so2,鎖,钅,18,12,金
镕=镕,róng,jung4,鎔,钅,18,15,金
镉=镉,gé,gaak3,鎘,钅,18,15,金
镑=镑,bàng,bong2、bong6,鎊,钅,18,15,金
镈=镈,bó,bok3,鎛,钅,24,15,金
镐=镐,gǎo,hou6,鎬,钅,18,15,金

顽固不化 发表于 2009-9-29 22:50:32

哈哈,频繁刷新页面是被禁止的。

afan 发表于 2009-9-29 22:56:14

哈哈,频繁刷新页面是被禁止的。
5260
顽固不化 发表于 2009-9-29 22:50 http://www.autoitx.com/images/common/back.gif

看来顽固兄测试过猛~ 呵呵

ac5474012 发表于 2009-9-29 22:59:21

啊 太好了,刚刚在啃正规表达式呢,看看顽固不化的这个~
页: [1] 2
查看完整版本: 求助如何获取一段网页中的文字