Read out Informations of a DOM buildet website

Discussion in 'PHP' started by Oliver Fahrni, Nov 12, 2013.

  1. #1
    Dear Programming Community,

    I need your help, i need some data from an external website (http://www.srf.ch/sport/resultate/fussball/super-league), i want to have the results in variables. I tried with file_get_content but the datas aren't stored in html. With DOM I got problems to do that.
    Thank you for your help
     
    Oliver Fahrni, Nov 12, 2013 IP
  2. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #2
    It's becoming more and more common that people are making posts asking how to scrape. I suggest that you contact the site owner and ask for feed/API access (they might charge a fee) and you will then be able to retrieve the data much more easily. If they don't have that, at least get permission.

    Otherwise, stop being lazy and use search as this question has been answered many times (including recently). In fact, there is a thread about it within this part of the forum, further down the thread list.
     
    ryan_uk, Nov 12, 2013 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    While I don't mind being helpful, there seems to be more and more of the "I need this, fix it for me" posts. No, that's not how this works. You provide what code you have, preferably in a testable way, or at least with proper urls and proper code in actual code-brackets, and then, and only then, can we try to figure out where you've gone wrong.

    And, here I was, thinking Google gets accused all the time for taking over the world, and yet there seems to be a complete lack of understanding how to use Google's main asset - its search engine.
     
    PoPSiCLe, Nov 12, 2013 IP
    ryan_uk likes this.
  4. MakZF

    MakZF Well-Known Member

    Messages:
    390
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    140
    #4
    Use PHP's DOM classes?
     
    MakZF, Nov 12, 2013 IP
  5. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #5
    Yes, webdevelopers and so-called SEO experts should be an expert in using a search engine and finding information. Shock horror that they totally suck. Even when my stepdaughter pretty much well just knew "hi, how are you?" she managed to use Google UK to search in English and find toys for me to buy for her lol.
     
    ryan_uk, Nov 12, 2013 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    That would be pretty easy I hope. As page is giving ajax calls and populating content from external links you need to monitor external links [using firebug in firefox browser], such as examine this one : http://www.srf.ch/swisstxt/resultate/fussball/super-league/2014/rnd_regular_act.html

    If you cant read that one using file_get_contents use CURL to read it. Or let me know if you need a parser. Test I did worked [quick test]
    <?php
    
        $data = file_get_contents('http://www.srf.ch/swisstxt/resultate/fussball/super-league/2014/rnd_regular_act.html');
      
        file_put_contents('test.html', $data);
    
    PHP:
    regards
     
    Vooler, Nov 12, 2013 IP
  7. Oliver Fahrni

    Oliver Fahrni Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #7
    Well sorry for my English but I do need you help... And yes I got the permission to copy the data from the webmaster and thank for the tip to search in Google for it, didn't have the idea before :) The threads down the list are so far I've seen all about to scrape data between HTML-Tags and convert them in a DOM. I wrote in here because i couldn't find what I need, but now I got a start. Thanks to Vooler.
     
    Oliver Fahrni, Nov 12, 2013 IP
  8. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #8
    Dear Oliver, you are welcome. When you say DOM object, in fact it is a DOM object already. At PHP-end you need to parse it to use it as real Object in PHP as :
    <?php
        $data = file_get_contents('http://www.srf.ch/swisstxt/resultate/fussball/super-league/2014/rnd_regular_act.html');
        $doc = new DOMDocument();
        $doc->loadHTML($data);
        #....... AND SO ON
    PHP:
    The only thing in start that you have to do is finalize the url's you have to fetch data from :).

    Stay well....
     
    Vooler, Nov 12, 2013 IP
  9. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #9
    Solution:

    Put the attachment in same folder, save following code as test.php, and run test.php. It will output as Array...

    <?php
        $data = file_get_contents('http://www.srf.ch/swisstxt/resultate/fussball/super-league/2014/rank_regular_act.html');
        include 'simple_html_dom.php';
       
        $theDoc = str_get_html($data);
        $oTRs = $theDoc->find('table tr');
        $aData = array();
        foreach($oTRs as $oTR) {
            $aRow = array();
            $oTDs = $oTR->find('td');
    
            foreach($oTDs as $oTD) {
                $aRow[] = trim($oTD->plaintext);
            }
    
            $aData[] = $aRow;
        }
       
        print_r($aData);
    
    PHP:
     

    Attached Files:

    Vooler, Nov 12, 2013 IP