simple_html_dom 使用
2022-04-23 本文已影响0人
云龙789
https://sourceforge.net/projects/simplehtmldom
https://simplehtmldom.sourceforge.io/docs/1.9/api/file_get_html/
/**
* @param $file_name string 请求 url
* @param $type_name string 类型名字
*/
private function getVideoInfo($file_name, $type_name)
{
$website = 'https://xxx';
$html = file_get_html($file_name);
$dom = $html->find('div[class=videoListBox]');
foreach ($dom as $item) {
foreach ($item->find('a') as $element) {
$arr = [
'title' => $element->find('p[class=title]', 0)->innertext, // 标题
'img_add' => $element->find('img', 0)->src, // 图片
'video_add' => $element->href, // 视频地址
];
}
}
}
/**
*
* @param $file_name string 请求 url
* @param $type_name string 类型名字
*/
function getPic($file_name, $type_name = '')
{
$html = file_get_html($file_name);
// 1. 获取24组图片的 li 标签 productListContent-item
$list = $html->find('li[class=search-item col lg-1 md-1 sm-1 custom-hover not-fashion-flex]');
$arr = [];
foreach ($list as $k => $item) {
// 2. 找到第一个 img
$imgInfo = $item->find('img',0);
$img_url = ($imgInfo->{'data-src'});
$price = ($item->find('div[class=first-price-area]',0))->find('span',0)->innertext;
$name = ($item->find('p[class=hb-pl-cn]',0))->find('span',0)->innertext;
$name = html_entity_decode($name);
//$url = $this->imgWrite($img_url);
}
return $arr;
}
foreach ($list as $item) {
$img_url = $item->website . $item->img_add;
$img_info = file_get_contents($img_url);
// $file_name = 'tmp_img_yang/' . $item->resource_id . '.png';
$file_name = 'tmp_img_yang/' . $item->hash . '.png';
$resource = fopen($file_name, 'a');
$res = fwrite($resource, $img_info);
fclose($resource);
}