Jump to content

help with scraping (fopen ...)


billy2shoe

Recommended Posts

hi there...

I aim trying to do some code that basically reads a web-page and extracts certain information from it

 

first: I am trying to write some code that reads a URL .. and prints the contest of the site out

 

why doesnt this code work below????

I don't think the fopen("$URL", "r"); is working correctly

 

=================================================

<?

$URL = "http://www.dur.byethost4.com/screen_two/r_rules.php";

$file = fopen("$URL", "r");

$r = "";

do{

    $data = fread($file, 8192);

    $r .= $data;

    echo "data: $data \n";

}

while(strlen($data) != 0);

?>

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/36868-help-with-scraping-fopen/
Share on other sites

<?

$URL = "http://www.dur.byethost4.com/screen_two/r_rules.php";

$file = fopen($URL, "r");

$r = "";

do{

    $data = fread($file, 8192);

    $r .= $data;

    echo "data: $data $r\n";

}

while(strlen($data) != 0);

?>

 

Okay i have made this change..but why do i not get any out put from this logic above?

Instead of using fopen and fread to get the contents of a website, just use file_get_contents (http://www.php.net/file_get_contents), or file (http://www.php.net/file). 

 

Either of those will read the entire "text" (html) of the website into a variable (string in the case of the former, array for the latter).

hmmm interesting ...

 

i read that site u sent me and use the code (example) ...and it doesnt work .. perhaps im doing something wrong ..  the link is: http://au2.php.net/file

 

(this is the code)

 

<?php

 

$lines = file('http://www.example.com/');

foreach ($lines as $line_num => $line) {

  echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";

}

 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.