Get language of a website using simple html dom

邮差的信 提交于 2019-12-13 09:23:45

问题


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();
                }
            }

        } 

回答1:


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.



来源:https://stackoverflow.com/questions/28058638/get-language-of-a-website-using-simple-html-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!