我如何用经典的asp xmlhttp解析html描述元

时间:2017-06-27 16:26:22

标签: html regex asp-classic regexp-like

我使用经典代码与解析html desctiption meta是:

Html exaple:

<meta name="keywords" content="Software AG, Altenkesseler Straße 17, Saarbrücken, Saarland, software ag, Ofis"  />

解析代码:

    Baslangic = InStr(1,sdata,"<meta name=" & Chr(34) & "keywords" & Chr(34), 1) + Len("<meta name=" & Chr(34) & "keywords" & Chr(34)) 
    Baslangic = InStr(Baslangic,sdata,Chr(34),1)+1 
    Genislik = InStr(Baslangic,sdata,Chr(34),1) - Baslangic

KeywordAl= Mid(sdata, Baslangic, Genislik) 

这是有效的,但是当html代码更改着作的位置时,例如:

<meta  content="Software AG, Altenkesseler Straße 17, Saarbrücken, Saarland, software ag, Ofis" name="keywords" />

它不起作用我的代码。有没有办法在两边跑?正则表达式或我的方式。

谢谢

2 个答案:

答案 0 :(得分:0)

if (instr(1, sdata,"<meta ", 1)=1 and instr(7, sdata, "name=""keywords""", 1)>6) then
  Set regEx = New RegExp
  regEx.Pattern = "content=""(.+?)"""   
  regEx.IgnoreCase = True
  Set matches = regEx.Execute(sdata)
  if matches.Count > 0 then
    KeywordAl = matches(0).SubMatches(0)
  end if
end if

答案 1 :(得分:0)

你可以尝试这样的事情:

meta = "<meta name=""keywords"" content=""Software AG, Altenkesseler Straße 17, Saarbrücken, Saarland, software ag, Ofis""  />"
if InStr( meta, "content=" ) > 0 then
    arrMeta = Split( meta, "content=" )
    keywords = arrMeta( 1 )       ''-- your data will be in the 2nd row of the array
    keywords = Trim( Replace( Replace( Replace( keywords, """", "" ), "/>", "" ), "name=keywords", "" ) )
end if

Response.Write keywords