Getting Remote Pages with ASP - A Little Parse-ly on the Side
(Page 3 of 4 )
You may just need to save the file locally, and nothing more. If that's the case, feel free to skip right to the clean-up section. But most likely you'll need to do some work on the file now that we have it.
I'm going to show you how I read the file line-by-line, looking for indicators of specific columns of information. Looking back, I would for sure use regular expressions to do this searching were I to re-write the script. If you're only looking for a couple of items, this is one way to tackle it, but for anything more I would highly recommend using a regular expression.
If Not objFSO.FileExists("csv.txt") Then objFSO.CreateTextFile("csv.txt")
Set objFile = objFSO.GetFile("csv.txt")
Set objWrite = objFile.OpenAsTextStream( 2, -2 )
Set objFile2 = objFSO.GetFile("skaters.txt")
Set objRead = objFile2.OpenAsTextStream( 1, -2 )
So we've opened the file for reading, and created a file to retain the extracted data in comma separated values. Now we just need to define exactly what we want. I had it easy because the columns of data that I wanted to start with all had a similar class. So I just skipped through all the content in the head and body of the web page that occurred before the data and then started my work. You may have to examine the downloaded file and figure out your own plan of attack.
strSearch = "<tr class=""ysprow"
thisLine = ""
dim firstName, lastName, pts
Do Until Left(thisLine, Len(strSearch)) = strSearch And Not objRead.AtEndOfStream
thisLine = objRead.ReadLine
Loop
Now I do the extraction of the first name, last name, and points. This could be somewhat problematic if the information you seek is not in such a well defined format. But mine was, so here's how I did it:
While Not objRead.AtEndOfStream
If Left(thisLine, Len(strSearch)) = strSearch Then '=== name
thisLine = objRead.ReadLine
'=== trimming
thisLine = Mid(thisLine, InStr(thisLine, "<a"))
thisLine = Mid(thisLine, InStr(thisLine, """>") + 2)
thisLine = Left(thisLine, InStr(thisLine, "</a>") -1)
'=== extract first name
firstName = Left(thisLine, InStr(thisLine, " ") -1)
'=== extract last name
lastName = Right(thisLine, len(thisLine) - Len(firstName) - 1)
End If
'=== now search for points column
If InStr(thisLine, "td class=""ysptblclbg6""") > 0 Then 'points
'=== trim
thisLine = Mid(thisLine, InStr(thisLine, "<span class=""yspscores"">") + 24)
thisLine = Left(thisLine, InStr(thisLine,"<") - 1)
'=== extract points
pts = thisLine
objWrite.WriteLine( firstName & "," & lastName & "," & pts )
End if
thisLine = objRead.ReadLine
Wend
objRead.Close
Next: Clean Up After Yourself! >>
More ASP Articles
More By Justin Cook