Collecting and storing Screen Scraping Data

Discussion in 'PHP' started by archers, Jun 15, 2012.

  1. #1
    Hey,

    Just asking for some help with scraping data off a webpage, and storing to a text/csv/xml file, or whatever works best.

    So this is my basic code that scraped the info i want off a webpage:
    Does anybody know a php function that allows me to print data to a file?

    Any help would be much appreciated, thanks.
     
    archers, Jun 15, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2

    I hate posts like these. What would you do if you didn't have a forum to go directly to? You would use google. Google PHP File Operations. You will find an easy tutorial on how to create and manipulate text files.

    Personally I would use a database to store data from a scraper.
     
    NetStar, Jun 15, 2012 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Open the file you want to save to. Write each element of the array to the file. Close the file.
     
    Rukbat, Jun 15, 2012 IP
  4. yelbom

    yelbom Greenhorn

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    Here is a example for scraping site data using php:


    $string = file_get_contents("http://example.com");
    preg_match_all('%<span class="tag">(.*?)</span>%sim', $string, $result);
    $result = $result[1];
    
    foreach($result as $item){
            $item = preg_replace('%<span class="tag-chain-item-span">(.*?)</span>%sim', '$1', $item);
            $display .= '<a href="?search='.$item.'">'.$item.'</a><br>';
            }
    echo $display;
    
    PHP:
     
    yelbom, Dec 9, 2012 IP
  5. davetrebas

    davetrebas Active Member

    Messages:
    301
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    78
    #5
    You could also start with an empty string. $out = "";

    Then gather up all the elements in the loop $out .= "$element\r\n";

    After the loop do a file_put_contents("file.txt", $out);
     
    davetrebas, Dec 11, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    In fact, yelbom's code will throw an undefined variable error at the first execution of

    $display .=

    since $display hasn't been defined yet. (.= doesn't define a variable the way = does.)
     
    Rukbat, Dec 11, 2012 IP