开发者

Weird issue with Simple HTML DOM and extracting the data through a nested loop

I apologize if this is a newbie question, but I cannot figure out why this doesn't work - and I can't seem to find anything about it when searching.

Basically, I am trying to scrape some userdetails from our site, that are not available from the sites REST api, so I have to do it manually. I have compiled a textfile with userids, that I use for fetching the wanted details from each user through Simple HTML Dom.

<?php
include('simple_html_dom.php') ;
include('functions.php') ;

$file = fopen("userids2.txt", "r") ;
while(!feof($file)) {
    $userid = fgetss($file) ;
    $url = 'http://<our URL>/user/'.$userid ;
    echo $url ; 
    webscraper($url) ;

}

fclose($file) ;
?>

and here are the contents of functions.php:

   <?php
function webscraper($loopurl) {
    $html = new simple_html_dom();
    $html->load_file($loopurl);
    $test = $html->getElementsById('ctl00_ContentPlaceHolderDefault_UserViewUC_tabContainer_tabProfile_userProfile_ddWork') ;

    foreach ($test as $element) {
        echo $element ;
    }
}
?>

The specific textfile used contains 4 userids that I know contain the information that I want. When I run the script it will only give me the output from the url from the last line in the textfile. It prints out the URLs fi开发者_JAVA百科ne, but refuses to load the remote html for the first three entries. If I delete the last line of the textfile, it then loads the new last line (which it refused to do before).

Any ideas?? Thanks in advance.


Doh.. I found out what the problem was. There was an "invisible" end of line character on all entries in the textfile EXCEPT the last one. So that was why it refused to work. Adding trim when retrieving the line fixed the problem:

 $userid = trim(fgetss($file)); 

I probably should have known this, but at least I won't make this mistake next time :-).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜