Screen Scrape Help

driveflexfuel

New Member
Messages
159
Reaction score
0
Points
0
I am new to working with this type of code any assistance is greatly appreciated. Below is the code that I am trying to get working. I am stuck on the portion where I add the data to the database.

Code:
//Connect to page and retreive content
$url = "http://www.site.com";
$raw = file_get_contents($url);

// remove all lines from the code
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));

// Select the table that will be used
$start = strpos($content,'<TABLE NAME="results" border="0" cellpadding="2" cellspacing="0" width="660">');
$end = strpos($content,'</TABLE>',$start) + 8;
$table = substr($content,$start,$end-$start);


preg_match_all("|<tr(.*)</tr>|U",$table,$rows);

foreach ($rows[0] as $row){

    if ((strpos($row,'<TH')===false)){
  
        preg_match_all("|<TD(.*)</TD>|U",$row,$cells);
      
        $list1 = strip_tags($cells[0][0]);
        $list2 = strip_tags($cells[0][1]);
        $list3 = strip_tags($cells[0][2]);
          
    }

}
mysql_query("INSERT INTO station (name, address, city, state, zip, phone, notes) VALUES ('$name', '$address', '$city', '$state', '$zip', 'phone', 'notes')") or die(mysql_error());
This is a sample of what the coding looks like after $content
Code:
<TABLE NAME="results" border="0" cellpadding="2" cellspacing="0" width="660"><TR><TD valign="top" class="tdmedium" colspan="3"><P><CENTER><B class="bigblue">All E85 Pumps in Wisconsin</B></CENTER></P></TD></TR><tr><TD class="tdbasic" valign="top"><B>Pump 24</B><BR>451 Main St.<BR>Adams, WI 53910<BR>608-339-3626<BR><BR><BR></TD><TD class="tdbasic" valign="top"><B>Holiday Station</B><BR>Highway 8<BR>Almena, WI 54805<BR>715-357-3957<BR><BR><BR></TD><TD class="tdbasic" valign="top"><B>Kwik Trip #451</B><BR>455 State Hwy 64<BR>Antigo, WI 54409<BR>715-623-6655<BR><BR><BR></TD></tr><tr><TD class="tdbasic" valign="top"><B>Kwik Trip #452</B><BR>3721 W. College Ave.<BR>Appleton, WI 54914<BR>920-993-9089<BR><BR><BR></TD><TD class="tdbasic" valign="top"><B>Express Convenience Center</B><BR>2175 South Memorial Drive<BR>Appleton, WI 54915<BR>920-380-0492<BR><BR><BR></TD><TD class="tdbasic" valign="top"><B>Sanborn Station</B><BR>411 Sanborn Ave<BR>Ashland, WI 54806<BR>715-682-2745<BR><BR><BR></TD></tr></TABLE>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
I'm not seeing any real problem with the insert, unless one of the pieces of data you are trying to insert is bad, ie unescaped characters. Where are $name, $address...$notes defined? Also, I'm guessing $notes and $phone are variables and not the words 'notes' and 'phone'.

When I can't get a query to work, I usually try doing it manually in phpmyadmin. It gives more helpful error messages. When that works, then I go back and work it into the code.
 
Top