找回密码
 加入
搜索
查看: 1897|回复: 4

[AU3基础] [已解决]有没有更好的办法格式化网页源码?

[复制链接]
发表于 2010-7-18 16:00:09 | 显示全部楼层 |阅读模式
本帖最后由 itljl 于 2010-8-18 12:29 编辑

我们要将
$string = '◇ <A href="http://bbs.cctv.com/read.php?tid=112869" target=main>中华人民共和国万岁</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112868" target=main>一切反动派都是纸老虎</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112867" target=main>社会主义比资本主义优越五倍</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112866" target=main>因为社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112865" target=main>为什么社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112864" target=main>因为社会主义比资本主义优越</A> <BR>'
这样一串字符串格式化一个二维数组


我用的方法是:
#include <array.au3>

Global $NewPost[1][2]



$string = '◇ <A href="http://bbs.cctv.com/read.php?tid=112869" target=main>中华人民共和国万岁</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112868" target=main>一切反动派都是纸老虎</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112867" target=main>社会主义比资本主义优越五倍</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112866" target=main>因为社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112865" target=main>为什么社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112864" target=main>因为社会主义比资本主义优越</A> <BR>'

;拆分得到的数据
$var = StringSplit($string, "◇")


For $i = 2 To $var[0]
        ;MsgBox(0, 0, $var[$i])

        ;得到标题
        $Str_s = 'n>'
        $Str_e = '</'
        $sReturn_txt = StringRegExp($var[$i], $Str_s & '(.*)' & $Str_e, 3)
        ;MsgBox(0, 0, $sReturn_txt[0])

        ;得到URL
        $Str_s = '="'
        $Str_e = '" target'
        $sReturn_url = StringRegExp($var[$i], $Str_s & '(.*)' & $Str_e, 3)
        ;MsgBox(0, 0, $sReturn_url[0])


        ;将得到的标题和URL增加到二维数组中
        _ArrayAdd2($NewPost, $sReturn_txt[0], $sReturn_url[0])

Next

;显示得到的二维数据
_ArrayDisplay($NewPost)
哪位兄弟有更好,更简洁的方法吗?

本帖子中包含更多资源

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

×
发表于 2010-7-18 16:57:29 | 显示全部楼层
#include <array.au3>
$string = '◇ <A href="http://bbs.cctv.com/read.php?tid=112869" target=main>中华人民共和国万岁</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112868" target=main>一切反动派都是纸老虎</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112867" target=main>社会主义比资本主义优越五倍</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112866" target=main>因为社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112865" target=main>为什么社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112864" target=main>因为社会主义比资本主义优越</A> <BR>'
$Array=StringRegExp($string,'(?i)<A.*?<\/A>',3)
Local $Fin[UBound($array)][2];根据正则整理定义数组
For $i=0 To UBound($Array)-1
        $SpliArray=StringRegExp($Array[$i],'<.*?>',3)
        $UrlArray=StringRegExp($SpliArray[0],'"[^"].*?"',3);明明指明了不包含双“”号的,怎么还有?
        $Fin[$i][1]=$UrlArray[0]
        $Fin[$i][0]=StringRegExpReplace($Array[$i],'<.*?>','')        
Next
_ArrayDisplay($Fin)

评分

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

查看全部评分

发表于 2010-7-18 17:16:47 | 显示全部楼层
#include <Array.au3>
Local $string = '◇ <A href="http://bbs.cctv.com/read.php?tid=112869" target=main>中华人民共和国万岁</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112868" target=main>一切反动派都是纸老虎</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112867" target=main>社会主义比资本主义优越五倍</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112866" target=main>因为社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112865" target=main>为什么社会主义有先进的文化,科技</A> <BR>◇ <A href="http://bbs.cctv.com/read.php?tid=112864" target=main>因为社会主义比资本主义优越</A> <BR>'
Local $sR = StringRegExp($string, 'href="(.+?)".+?main>(.+?)</', 3)
Local $NewPost[UBound($sR) / 2 + 1][2] = [[UBound($sR) / 2]]
For $i = 0 To UBound($sR) - 2 Step 2
        $NewPost[($i + 2) / 2][0] = $sR[$i + 1]
        $NewPost[($i + 2) / 2][1] = $sR[$i]
Next
_ArrayDisplay($NewPost)
 楼主| 发表于 2010-7-19 16:05:00 | 显示全部楼层
afan 发表于 2010-7-18 17:16



    谢谢分享新方法。
 楼主| 发表于 2010-7-19 16:05:10 | 显示全部楼层
3mile 发表于 2010-7-18 16:57



        谢谢分享新方法。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-3 14:31 , Processed in 0.087435 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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