Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts

    How to save results in .csv file?

    Hello,
    I have this working script and I would like to save those results in csv. Can anyone help me how to do that?

    PHP Code:
    <?php
    $url
    ="http://www.ebay.com/sch/Diamonds-Natural-/3824/i.html?_dcat=3824&Clarity=IF%7CVVS1%7CVVS2&_ssn=gemstoneking&_pgn=2&_skc=200&rt=nc";
    $data=file_get_contents($url);
    $data strip_tags($data,"<a>");
    $d preg_split("/<\/a>/",$data);
    foreach ( 
    $d as $k=>$u ){
        if( 
    strpos($u"<a href=") !== FALSE ){
            
    $u preg_replace("/.*<a\s+href=\"/sm","",$u);
            
    $u preg_replace("/\".*/","",$u);
            print 
    $u."\n";
        }
    }
    Thanks...

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Which data are you trying to get ...
    title?
    price?
    ?

    And are you OK with only grabbing the first 50 results?

    You can specify page, but the number per page is 50.
    It looks like there are 10 pages ... about 500 in total?


    .
    Last edited by mlseim; 01-23-2013 at 06:52 PM.

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hello Mlseim- thank you for your reply.
    It would be great only to save urls of those products which script generates (those urls which starts with "http://www.ebay.com/itm/") ....

    I think I can handle from which page it would take products.

    Thank you for your help- I really appreciate it!
    Last edited by Scharfheimlich; 01-23-2013 at 10:51 PM.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Upload the script below and see what happens.
    That might give you a nice start on it.
    If that script yields good results, you can tweak it and work on the csv part.

    PHP Code:

    <?php 
    $url
    ="http://www.ebay.com/sch/Diamonds-Natural-/3824/i.html?_dcat=3824&Clarity=IF%7CVVS1%7CVVS2&_ssn=gemstoneking&_pgn=1&_skc=200&rt=nc"
    $data=file_get_contents($url);
      
    // pattern
    $pattern '{<div\s+class="ittl"\s*>((?:(?:(?!<div[^>]*>|</div>).)++|<div[^>]*>(?1)</div>)*)</div>}si';
     
    $matchcount preg_match_all($pattern$data$matches);
    echo(
    "<pre>\n");
    if (
    $matchcount 0) {
        echo(
    "$matchcount matches found.\n");
        for(
    $i 0$i $matchcount$i++) {
            echo(
    "\nMatch #" . ($i 1) . ":\n");
            echo(
    $matches[1][$i]); 
        }
    } else {
        echo(
    'No matches');
    }
    echo(
    "\n</pre>");


    ?>
    This is not my script, by the way ... I could never figure out that regex by myself.
    Just something I remembered that I had in my pile of snippets.

    The key to the script, is the fact that all descriptions are contained in this tag: <div class="ittl"> blah blah blah </div>



    .
    Last edited by mlseim; 01-23-2013 at 11:21 PM.

  • Users who have thanked mlseim for this post:

    Scharfheimlich (01-24-2013)

  • #5
    New Coder
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thank you, that was easier than my script...

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The CSV part is a matter of creating a new file (open a file for writing, such as "ebay.csv"), and then looping through the array and writing each line to that file. Each line is followed by a carriage return. If there are more than one thing for each line, such as title and price, they would be separated by a comma ... but caution used if commas are part of your item title. You need to choose the best delimiter.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •