Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

php - Get language of a website using simple html dom

I am building a search engine and webcrawler using PHP, and i would like to detect the language of a website, how would i detect the language of a page by:

  1. Checking the URL for https://twitter.com/?lang=jap
    if that is not set then i would like to:
  2. Check the URL https://www.google.co.jp/

if i still can't find anything then i would to set default to English

the code i have so far for scraping pages is:

function crawl($url){
            $html = file_get_html($url);
            if($html && is_object($html) && isset($html->nodes)){
                $weblinks[]=$url;
                foreach($html->find('a') as $element) {
                    global $weblinks;
                    $link = $element->href;
                    $base_url = parse_url($url, PHP_URL_HOST);
                    if(substr($link,0,7)=="http://"){
                        $link = $link;
                    }else if(substr($link,0,8)=="https://"){
                        $link = $link;
                    }else if(substr($link,0,2)=="//"){
                        $link = substr($link, 2);
                    }else if(substr($link,0,1)=="#"){
                        $link = $html;
                    }else if(substr($link,0,7)=="mailto:"){
                        $link = "";
                    }else if(substr($link,0,11)=="javascript:"){
                        $link = "";
                    }else{
                        if(substr($link, 0, 1) != "/"){
                            $link = $base_url."/".$link;
                        }else{
                            $link = $base_url . $link;
                        }
                    }
                    if(substr($link, 0, 7) != "http://" && substr($link, 0, 8) != "https://" && $link != ""){
                        if(substr($url, 0, 8) == "https://"){
                            $link = "https://".$link;
                        }else{
                            $link = "http://".$link;
                        }
                    }
                    if(!in_array($link, $weblinks)){
                        $weblinks[]=$link;
                    }
                }
                $html->clear();
            }else{

            }
        }
        function info($weblinks){
            foreach($weblinks as $link) {
                $linkhtml = file_get_html("$link");
                if($linkhtml && is_object($linkhtml) && isset($linkhtml->nodes)){

                    $titleraw = $linkhtml->find('title',0);
                    $title = $titleraw->innertext;
                    $des = $linkhtml->find("meta[name='description']",0)->content;


//detect language here

                    echo "<tr><td>".$title."</td><td>".$link."</td><td>".$des."</td></tr>";
                    $sql = mysql_query("INSERT into web once");
                    $title = "";
                    $des = "";
                    $linkhtml->clear();
                }
            }

        } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get the language from ?lang=:

$url = 'www.domain.org?lang=IT';
$url_parts = parse_url($url);
$lang = parse_str($url_parts['lang']);

You should then validate this with a switch/case statement and a list of languages that you support, like this:

switch ($lang) {
case 'EN':
//language is English
break;
case 'IT':
//language is Italian
break;
case 'FR':
//language is French
break;
default:
//?lang query was empty, or contained an unsupported language
$lang = FALSE;
} //end switch

After that, you can use this logic to determine whether you need to check the URL for the language:

if ($lang == FALSE) {
//code to determine language from TLD
}

Hopefully this will help get you started, although this is a big can of worms you're opening up. There are other things you need to check in order to be certain of the language of a website in addition to what you've mentioned. One of them is the language meta tag, which is like this: <meta name="language" content="english"> and goes in the head of the webpage, though not all websites use it.

Some multilingual websites, like mine, use a subdomain like http://it.website.com or http://fr.website.com

Others use query strings that are different from ?lang=. So you'll need to do a significant amount of research to cover all your bases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...