webForumDet fria alternativet

Hämta värde ifrån en annan webbplats?

ASP

6 svar · 411 visningar · startad av Quid

Medlem sedan juni 20006 022 inlägg
Frågan#1

Jag ska hämta två värden ifrån en annan webbplats.

Först så hämtar jag hela sidan med XMLHTTP:

Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP") objXMLHTTP.Open "GET", "http://www.mindoman.se/", False objXMLHTTP.Send() v_scrape = objXMLHTTP.responseText Set objXMLHTTP = Nothing

Sen så tänkte jag köra regexp för koden för att få ut de två värdena som jag vill ha. Är detta rätt tänkt eller finns det smidigare sätt att lösa det på?

Det jag får ut då är ju HTML-koden, och mitt i all denna kod så finns följande:

var sr = new Array;
sr[0] = '7 °C';
sr[1] = '11 °C';

Jag vill ha ut 7 °C och 11 °C, hur löser man detta?
Jag är sjukt kass på patterns i regexp, någon som kan bistå med lite hjälp?

Medlem sedan juni 20019 519 inlägg
#2
Dim re, myMatches, myMatch
Set re = New RegExp
re.IgnoreCase = True
re.MultiLine = True
re.Pattern = "sr\[\d+\] = '([\s\S]*?)';"
Set myMatches = re.Execute(SubjectString)

For Each myMatch In myMatches
	Response.Write myMatch.SubMatches(0) & "<br />" ' 7&deg;C and 11 &deg;C
Next
Medlem sedan juni 20006 022 inlägg
#3

Funkade fint, men jag får bara ut det första värdet.

Medlem sedan juni 20019 519 inlägg
#4

ahhh

Dim re, myMatches, myMatch
Set re = New RegExp
re.IgnoreCase = True
re.MultiLine = True
[b]re.Global = True[/b]
re.Pattern = "sr\[\d+\] = '([\s\S]*?)';"
Set myMatches = re.Execute(SubjectString)

For Each myMatch In myMatches
	Response.Write myMatch.SubMatches(0) & "<br />" ' 7&deg;C and 11 &deg;C
Next
Medlem sedan juni 20006 022 inlägg
#5

Magi! Supernajs! Tack!

Har du lust att försöka förklara exakt vad som händer? Det hade varit trevligt att kunna förstår sånt här själv så jag slipper fråga varje gång :D

Medlem sedan juni 20019 519 inlägg
#6
Dim re, myMatches, myMatch
Set re = New RegExp				' Creates the Regular Expression Object
re.IgnoreCase = True				' We dont want to worrie about case sensitive content
re.MultiLine = True				' The search will/can be on more lines
re.Global = True				' All Results, or just the first result
re.Pattern = "sr\[\d+\] = '([\s\S]*?)';"	' * Look at Comments below 
Set myMatches = re.Execute(SubjectString)	' Get all Matches

For Each myMatch In myMatches			' Loop the matches
						' Print the Matches:
	Response.Write myMatch.SubMatches(0) & "<br />" ' 7&deg;C and 11 &deg;C (backreference number 1)
Next

Set Re = Nothing
Set myMatches = Nothing

' * RegExp RegExp Buddy Explains ******************
' sr\[\d+\] = '([\s\S]*?)';
' 
' Match the characters “sr” literally «sr»
' Match the character “[” literally «\[»
' Match a single digit 0..9 «\d+»
'    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
' Match the character “]” literally «\]»
' Match the characters “ = '” literally « = '»
' Match the regular expression below and capture its match into backreference number 1 «([\s\S]*?)»
'    Match a single character present in the list below «[\s\S]*?»
'       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
'       A whitespace character (spaces, tabs, line breaks, etc.) «\s»
'       Any character that is not a whitespace character «\S»
' Match the characters “';” literally «';»

Hoppas det lite klarare bild ;)

Medlem sedan juni 20006 022 inlägg
#7

Lysande! Tack så mycket!

268 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
132 ms — deklarationer (db)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)
124 ms — ändringar (db)