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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help on scraping data off a webpage using preg.match and cURL

    I am trying to echo a specific piece of variable data off a webpage and I cannot manage to do it (page is http://www.myfxbook.com/members/Nick...profits/282668). I can either echo the entire webpage, the links or the images, but not a specific variable off the page. The data I want is the +20.77%

    Code:
     <li class="paddB5 borderB"><span class="floatLeft dotted" id="absGainTip">Abs. Gain:</span>
                                        <span class="floatNone"><span class='green'>+20.77%</span></span></li>
    The code that I found that came closest was a pregmatch where I could find different parts of the title, body etc, but I only want the 'abs gain'

    Code:
     <?php
    
    $start = '<title>';
    
    $end = '</title>';
    
    $start = preg_quote($start, '/');
    $end = preg_quote($end, '/');
    
    $url = 'http://www.myfxbook.com/members/NickMcDonald/pound-dollar-profits/282668';
    
    $file = fopen("$url", "r"); 
    
    $document = fread($file, 80000);
    
    preg_match("/$start(.*?)$end/", $document, $matches);
    
    echo ($matches);  
    ?>
    Can anyone help me alter the code so that I can get what I need?

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    50
    Thanks
    6
    Thanked 4 Times in 4 Posts
    I got an better approach:

    #1 Use DOMdocument to load the html
    #2 Use XPath query to get spesific html tag.

    Note: this is just an example code I used 5min to create. So you need to tweak it.

    PHP Code:

    //error_reporting(0);

    $url 'http://www.myfxbook.com/members/NickMcDonald/pound-dollar-profits/282668';

    # DOMdocument
    $dom = new DOMDocument();
    $dom->loadHTMLfile($url);

    # XPath
    $xpath = new DOMXPath($dom);
    $tags $xpath->query('//ul[@class="quickStats"]/li/span/span[@class="green"]');
    $arr = array();
    foreach (
    $tags as $tag) {
        
    $arr[] = htmlentities($tag->nodeValue);
    }

    print_r($arr); 
    Output: Array ( [0] => +20.77% [1] => +20.77% [2] => $415.38 )
    Last edited by bjarneo; 10-21-2012 at 01:15 PM. Reason: Removed cURL. Not necessary.

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    50
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Just a question: Why do you not just use the API instead?
    http://www.myfxbook.com/api

    Since my code uses more than 2 seconds to get your result.
    Last edited by bjarneo; 10-20-2012 at 10:24 PM.


  •  

    Posting Permissions

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