Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2013, 01:17 PM   PM User | #1
Scharfheimlich
New Coder

 
Join Date: Jun 2011
Posts: 15
Thanks: 5
Thanked 0 Times in 0 Posts
Scharfheimlich is an unknown quantity at this point
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...
Scharfheimlich is offline   Reply With Quote
Old 01-23-2013, 06:47 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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..
mlseim is offline   Reply With Quote
Old 01-23-2013, 10:14 PM   PM User | #3
Scharfheimlich
New Coder

 
Join Date: Jun 2011
Posts: 15
Thanks: 5
Thanked 0 Times in 0 Posts
Scharfheimlich is an unknown quantity at this point
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..
Scharfheimlich is offline   Reply With Quote
Old 01-23-2013, 11:16 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
Scharfheimlich (01-24-2013)
Old 01-24-2013, 09:39 AM   PM User | #5
Scharfheimlich
New Coder

 
Join Date: Jun 2011
Posts: 15
Thanks: 5
Thanked 0 Times in 0 Posts
Scharfheimlich is an unknown quantity at this point
Thank you, that was easier than my script...
Scharfheimlich is offline   Reply With Quote
Old 01-24-2013, 12:15 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:59 AM.


Advertisement
Log in to turn off these ads.