$url = 'https://www.chemicalbook.com/Search.aspx?keyword='.$cas;
try{
//超时时间
$opts=array(
"http"=>array(
"method"=>"GET",
"timeout"=>15
),
);
////创建数据流上下文
$context = stream_context_create($opts);
$html = file_get_contents($url, false, $context);
$dom = new \DOMDocument();
//汉字乱码,在LoadHTML 之前先 对应的转码就可以了
$html = mb_convert_encoding($html ,'HTML-ENTITIES',"UTF-8");
@$dom->loadHTML($html);
$dom->normalize();
$xpath = new \DOMXPath($dom);
$lists = $xpath->query('/html/body//table[@id="mbox"]/tr');
$arr = [];
foreach ($lists as $node) {
$td = $xpath->query('./td//text()',$node);
if(strpos($td[0]->nodeValue, 'CBNumber')!== false){
$arr['CBNumber'] = $td[1]->nodeValue;
}
elseif(strpos($td[0]->nodeValue, '英文名称')!== false){
$arr['en'] = $td[1]->nodeValue;
}
elseif(strpos($td[0]->nodeValue , '中文名称')!== false){
$arr['cn'] = $td[1]->nodeValue;
}
elseif(strpos($td[0]->nodeValue , 'MF')!== false){
$arr['mf'] = $td[1]->nodeValue;
}
elseif(strpos($td[0]->nodeValue , 'MW')!== false){
$arr['mw'] = $td[1]->nodeValue;
}
elseif(strpos($td[0]->nodeValue , 'CAS')!== false){
$arr['cas'] = $td[1]->nodeValue;
}
}
//打印得出来的抓取内容的最终数组
Log::info('arr', $arr);
}catch (\Exception $e){
Log::info('searchChemBook exception', [$e]);
}