找回密码
 加入
搜索
查看: 1812|回复: 3

[AU3基础] [已解决]求正则取网页源代码指定内容

[复制链接]
发表于 2010-9-4 20:52:16 | 显示全部楼层 |阅读模式
本帖最后由 xyold1 于 2010-9-4 21:42 编辑

对正则还是不太熟悉,搜了一下相关的贴子还是没有解决,所以贴出来请帮下忙

谢谢
我想取http://dict.soso.com/d.q?sc=dict&ch=w.ptl&uin=&w=is中的 例句部分  的源代码,用的是以下代码
$Source = _INetGetSource('http://dict.soso.com/d.q?sc=dict&ch=w.ptl&uin=&w=is')
;~ $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
;~ $oHTTP.Open("GET","http://dict.soso.com/d.q?sc=dict&ch=w.ptl&uin=&w=troop")
;~ $oHTTP.Send()

;~ $Source = $oHTTP.Responsetext 
;~ MsgBox(0, '结果:', $Source)
;~ $Source=StringRegExpReplace ($Source, "\r\n", "|")
$sR = StringRegExp($Source, '例句与用法(.*)以上内容由', 3)
;~  MsgBox(0, '结果:', $sR[0])
If Not @Error Then MsgBox(0, '结果:', $sR[0])
Exit
没有成功,正则达人帮看一下错在哪里了


下面是网页部分源代码
<br><b>例句与用法</b><br><br class=x>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>1.  </td>
                <td>Pain past <b>is</b> pleasure. <br>过去的痛苦即是快乐。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>2.  </td>
                <td>A friend in need <b>is</b> a friend indeed. <br>患难之交才是真朋友。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>3.  </td>
                <td>The video <b>is</b> programmed to switch on at ten o'clock. <br>电视机设定在10点钟自动打开。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>4.  </td>
                <td>That's the way it <b>is</b>. <br>就是这么回事。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>5.  </td>
                <td>The room <b>is</b> 15 feet in length and 10 feet in breadth. <br>这房间长15英尺,宽10英尺。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>6.  </td>
                <td>There <b>is</b> no doubt about it. <br>那是毫无疑问的。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>7.  </td>
                <td>The boy <b>is</b> 150 centimeters tall. <br>这个男孩有150厘米高。<td>
                </table>
                        <table cellSpacing=0 cellPadding=0 class=mb8>
                <td valign=top>8.  </td>
                <td>Spain <b>is</b> our favorite holiday spot. <br>西班牙是我们喜欢的度假地点。<td>
                </table>
        
<br><span class="f12">以上内容由Dict.cn海词提供</span><br><br class=x>

评分

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

查看全部评分

发表于 2010-9-4 21:06:13 | 显示全部楼层
#include <array.au3>
#include <INet.au3>
$Source = _INetGetSource('http://dict.soso.com/d.q?sc=dict&ch=w.ptl&uin=&w=is')
;~ $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
;~ $oHTTP.Open("GET","http://dict.soso.com/d.q?sc=dict&ch=w.ptl&uin=&w=troop")
;~ $oHTTP.Send()

;~ $Source = $oHTTP.Responsetext 
;~ MsgBox(0, '结果:', $Source)
;~ $Source=StringRegExpReplace ($Source, "\r\n", "|")
Local $str=''
$sR = StringRegExp($Source, '<td>(.*<b>.*</b>.*)<br>(.*)<td>', 3)
For $i=0 To UBound($sR)-1 Step 2
        ;$sR[$i]=StringRegExpReplace($sR[$i],'<b>.*</b>','$1')
        $str&=StringRegExpReplace($sR[$i],'<b>.*</b>','$1')&@CRLF&$sR[$i+1]&@CRLF
Next
MsgBox(0,0,$str)
Exit

评分

参与人数 2金钱 +30 收起 理由
afan + 20
xyold1 + 10 谢谢

查看全部评分

 楼主| 发表于 2010-9-4 21:41:18 | 显示全部楼层
回复 2# 3mile

非常 感谢
辛苦了
 楼主| 发表于 2010-9-4 22:37:56 | 显示全部楼层
回复 2# 3mile

那个正则替换将关键字替换没了

下面是修正过的

#include <INet.au3>
$Source = _INetGetSource('http://dict.soso.com/d.q?sc=dict&ch=w.ptl&uin=&w=test')

Local $str=''
$sR = StringRegExp($Source, '<td>(.*<b>.*</b>.*)<br>(.*)<td>', 3)
For $i=0 To UBound($sR)-1 Step 2
        ;$sR[$i]=StringRegExpReplace($sR[$i],'<b>.*</b>','$1')
        $str&=StringRegExpReplace($sR[$i],'<b>|</b>','')&@CRLF&$sR[$i+1]&@CRLF
Next
MsgBox(0,0,$str)



Exit
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-3 08:20 , Processed in 0.097058 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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