正則表達試求助
我根據網頁寫一個正則表達式提取日期和數字,可是始終沒有顯示出來,可以幫我看看哪裡出了問題嗎?#include <ie.au3>
#include <INet.au3>
$oIE=_INetGetSource("http://fund.cnyes.com/fund_nav.asp?fundcode=B09%2C001&sdate=20050501&edate=20050531&Submit=%ACd%B8%DF")
Local $SHOW = ''
While 1
$Strings1=StringRegExp($oIE, "<td align=center>(.*?)</td>", 3)
If @error = 0 Then
$Offset1 = @extended
Else
ExitLoop
EndIf
$SHOW &= $Strings1& @CRLF
WEnd
MsgBox(0,"Message", $SHOW)
[ 本帖最后由 fancky 于 2009-3-10 17:08 编辑 ] 试试这个看看行不行.... 我这里测试可以用....
#include
#include
$oIE=_INetGetSource("http://fund.cnyes.com/fund_nav.asp?fundcode=B09%2C001&sdate=20050501&edate=20050531&Submit=%ACd%B8%DF")
$Strings1=StringRegExp($oIE, "(?<=align=center>).*?(?=)", 3)
$Strings2=UBound ($Strings1,1)-5
For $i=0 To $Strings2 Step 2
FileWrite ("test4.txt",$Strings1[$i] & @TAB & $Strings1[$i+1] & @CRLF)
Next #include <Inet.au3>
Local $url = "http://fund.cnyes.com/fund_nav.asp?fundcode=B09%2C001&sdate=20050501&edate=20050531&Submit=%ACd%B8%DF"
Local $str = _InetGetSource($url), $srePattern = "<td align=center>(?U)(.*)(?<! )</td>", $result, $var
$var = StringRegExp($str, $srePattern, 3)
For $o = 0 to Ubound($var) - 1
$result &= $var[$o] & @tab
If mod($o, 2) then $result &= @crLf
Next
msgBox(0, '', $result) 很奇怪,我在我的電腦使用_InetGetSource的方式來獲得原始碼一樣是出現空白沒有任何東西只有在桌面的右下角有出現AutoIt的運行符號,因此我另外嘗試使用_IEDocReadHTML的方式來獲得,結果是可以運行,但是始終抓不到數值,是哪裡出問題嗎?#include <IE.au3>
#include <Inet.au3>
$URL = 'http://fund.cnyes.com/fund_nav.asp?fundcode=B09%2C001&sdate=20050501&edate=20050531&Submit=%ACd%B8%DF/'
$oIE = _IECreate($URL, 0, 1, 1, 0)
$String = _IEDocReadHTML($oIE)
Local $Offset1 = 1,$SHOW = ''
While 1
$var = StringRegExp($String, '<td align=center>(?U)(.*)(?<! )</td>',3,$Offset1)
If @error = 0 Then
$Offset1 = @extended
Else
ExitLoop
EndIf
$SHOW &= $var & " " &@CRLF
WEnd
MsgBox(0,"數值", $SHOW)
[ 本帖最后由 fancky 于 2009-3-10 11:36 编辑 ]
#include <IE.au3>
$URL = 'http://fund.cnyes.com/fund_nav.asp?fundcode=B09%2C001&sdate=20050501&edate=20050531&Submit=%ACd%B8%DF/'
$oIE = _IECreate($URL, 0, 1, 1, 0)
$String = _IEDocReadHTML($oIE)
;$String=StringRegExpReplace ($String, "\r\n", "")
$array= StringRegExp($String,'<TD align=middle>(?U)(\d.*)</TD>', 3)
for $i = 0 to UBound($array) - 1
msgbox(0, "结果为" & $i, $array[$i])
Next
可以運作抓出數值了,不過我有疑問,為什麼bob大大的正則是寫成:<TD align=middle>(?U)(\d.*)</TD>,差別在<TD align=middle>,我原本表示的方法是不能用嗎?
[ 本帖最后由 fancky 于 2009-3-10 14:44 编辑 ]
.*?
===>
\d.*
恩恩,OK,我再來看看,謝謝了 你将_IEDocReadHTML读取到的源代码写到文本里看看
就清除了问什么是<TD align=middle>了 我用这种方法:
#include <INet.au3>
$url = "http://fund.cnyes.com/fund_nav.asp?fundcode=B09%2C001&sdate=20050501&edate=20050531&Submit=%ACd%B8%DF/"
$sHTML = _INetGetSource($url)
$aid = StringRegExp($sHTML, "<td align=center>(.*?)</td>", 3)
For $i = 0 To UBound($aid) - 1
ConsoleWrite($i & "--" & $aid[$i] & @CRLF)
Next
页:
[1]