2021年8月

相信很多phper们在使用composer添加新插件的时候,会碰到这个问题:”明明这次要安装的是一个新插件,结果提示其他插件安装报错“。
类似的报错如:

Your requirements could not be resolved to an installable set of packages.
Problem 1
    - phpoffice/phpspreadsheet is locked to version 1.7.0 and an update of this package was not requested.
    - phpoffice/phpspreadsheet 1.7.0 requires ext-zip * -> it is missing from your system. Install or enable PHP's zip extension.
  Problem 2
    - topthink/think-installer v2.0.0 requires composer-plugin-api ^1.0 -> found composer-plugin-api[2.1.0] but it does not match the constraint.
    - topthink/framework v5.1.23 requires topthink/think-installer 2.* -> satisfiable by topthink/think-installer[v2.0.0].
    - topthink/framework is locked to version v5.1.23 and an update of this package was not requested.

其实要解决这个问题很简单,只需要执行一下代码:

composer install --ignore-platform-reqs

composer update --ignore-platform-reqs

忽略掉composer的版本匹配,这样就可以继续正常安装新的包了,快去试试吧

都说网络爬虫就用python,但我不这么认为。
尤其是对于熟悉php又对python生疏的coder来说,其实完全不比舍近求远。
只要插件用的好,就没有php办不了的事儿,今天要给大家介绍的这款插件是PHP Simple HTML DOM Parser,html页面解析插件,效率之高,令人拍手叫绝(偷笑),而且操作简便,可以像jquery一样去获取html标签。实在是居家旅行,页面抓取,必备神器。

相关资源

操作手册:https://simplehtmldom.sourceforge.io/manual.htm
下载地址:https://sourceforge.net/projects/simplehtmldom/files/

获取远程地址html元素

// 新建一个dom实例
$html = file_get_html('http://www.baidu.com/');

// 获取所有img标签
foreach($html->find('img') as $element)
  echo $element->src . '<br>';

// 获取所有连接
foreach($html->find('a') as $element)
  echo $element->href . '<br>'; 

直接操作html字符串

// 根据字符串创建dom实例
$html = str_get_html('<html><body>Hello!</body></html>');

获取单个元素

// 获取第一个a标签元素
$ret = $html->find('a', 0);

// 获取最后一个a标签元素
$ret = $html->find('a', -1); 

获取元素值

// 创建Dom实例
$html = file_get_html('http://www.baidu.com/');

// 获取所有a标签的链接地址
foreach($html->find('a') as $e) 
    echo $e->href . '<br>';

// 获取所有img标签的src地址
foreach($html->find('img') as $e)
    echo $e->src . '<br>';

// 获取所有的img标签内容
foreach($html->find('img') as $e)
    echo $e->outertext . '<br>';

// 获取标签id=gbar的div标签内容
foreach($html->find('div#gbar') as $e)
    echo $e->innertext . '<br>';

// 获取标签id=gbar的div标签的文本信息
foreach($html->find('div#gbar') as $e)
    echo $e->plaintext . '<br>';

如何操作Dom树

// 示例
echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;



- 阅读剩余部分 -