欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

使用php-simple-html-dom-parser遇到的问题

程序员文章站 2022-05-16 20:03:51
...
今天使用这个工具:

https://github.com/sunra/php-simple-html-dom-parser

遇到了问题,首先我是用了php-simple-html-dom-parser自带的testcase中的slick_test.php,就报错,然后我自己写了三行最简单的代码来抓一下百度首页:

require'./simplehtmldom_1_5/simple_html_dom.php';
$html = file_get_html('http://www.baidu.com/');

//找到所有图片 foreach($html->find('img') as$element) 
       echo$element->src . '
'
; //找到所有链接 foreach($html->find('a') as$element) echo$element->href . '
'
;

结果报错为:

andy@AndyMacBookPro:/usr/local/webdata/github/php-simple-html-dom-parser/Src/Sunra/PhpSimple$ php andy.php
//www.baidu.com/img/baidu_jgylogo3.gif
//www.baidu.com/img/bd_logo.png
http://s1.bdstatic.com/r/www/cache/static/global/img/gs_237f015b.gif
//www.baidu.com/gaoji/preferences.html
/
https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
/
http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=
http://tieba.baidu.com/f?kw=&fr=wwwt
http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt
http://music.baidu.com/search?fr=ps&key=
http://image.baidu.com/i?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&word=
http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=
http://map.baidu.com/m?word=&fr=ps01000
http://wenku.baidu.com/search?word=&lm=0&od=0
//www.baidu.com/more/
javascript:;
javascript:;
javascript:;
http://w.x.baidu.com/go/mini/8/10000020
http://news.baidu.com
http://www.hao123.com
http://map.baidu.com
http://v.baidu.com
http://tieba.baidu.com
https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
//www.baidu.com/gaoji/preferences.html
//www.baidu.com/more/
http://news.baidu.com
http://tieba.baidu.com
http://zhidao.baidu.com
http://music.baidu.com
http://image.baidu.com
http://v.baidu.com
http://map.baidu.com
javascript:;
javascript:;
javascript:;
http://baike.baidu.com
http://wenku.baidu.com
http://www.hao123.com
//www.baidu.com/more/
/
//www.baidu.com/cache/sethelp/index.html
http://home.baidu.com
http://ir.baidu.com
/duty/
create_debugger_socket("localhost", 9000) socket: Too many open files
create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles create_debugger_socket("localhost", 9000) socket: Too many openfiles andy@AndyMacBookPro:/usr/local/webdata/github/php-simple-html-dom-parser/Src/Sunra/PhpSimple$

这个报错:

create_debugger_socket(“localhost”, 9000) socket: Too many open files

搞不懂,不知道哪里用到了socket连接??

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
现在搞定了,是这样的,首先我想调试一下出错的php看看哪里的问题,然后我开启了MacGDBP,发现开启了MacGDBp之后,竟然不报错了,又联想到了9000端口,这个是xdebug的对应调试端口,所以有了一点思路,可能是xdebug的问题,然后在网上参考了这里:

http://bugs.xdebug.org/view.php?id=1070

看到这里:

  • Installed Xdebug, and set xdebug.remote_connect_back and xdebug.remote_enable to 1 so that it connects to my host, running (or not running phpstorm)
  • Set xdebug.remote_log=/tmp/remote.log

然后我们到/etc/php.ini中为xdebug的配置添加了

xdebug.remote_log = /tmp/xdebug_remote.log

之后我们继续运行,继续出错,不过我们可以到/tmp/xdebug_remote.log文件中可以看到这种错误日志:

16 Log opened at2015-02-2003:48:2917 I: Connecting to configured address/port: localhost:9000.18 E: Could not connect to client. :-(
  19 Log closed at2015-02-2003:48:29

好吧,我们知道了无法连接到localhost:9000的socket端口,肯定的呀,因为我本地没有打开MacGDBP,这个是php的一个调试工具,如果你在php.ini的xdebug这里:

xdebug.remote_enable = on
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9000
xdebug.remote_autostart = 1

如果xdebug.remote_autostart设置为1,也就是自动开启的话,就会直接请求开启9000端口的socket连接,如果没有就报错。原因就是这样:-)。

好了,因为这个日志文件输出还挺大的,所以我们可以关闭xdebug.remote_log了,也就是:

xdebug.remote_enable = on
xdebug.remote_handler = dbgp
xdebug.remote_host = localhost
xdebug.remote_port = 9000
xdebug.remote_autostart = 1
;xdebug.remote_log = /tmp/xdebug_remote.log

加分号就ok了。

以上就介绍了使用php-simple-html-dom-parser遇到的问题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。