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

WBOY
发布: 2016-08-08 09:29:36
原创
1544 人浏览过

今天使用这个工具:

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

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

<code><span><span><?php </span><span>require</span><span>'./simplehtmldom_1_5/simple_html_dom.php'</span>;
<span>$html</span> = file_get_html(<span>'http://www.baidu.com/'</span>);

<span>//找到所有图片 </span><span>foreach</span>(<span>$html</span>->find(<span>'img'</span>) <span>as</span><span>$element</span>) 
       <span>echo</span><span>$element</span>->src . <span>'<br>'</span>;


  <span>//找到所有链接 </span><span>foreach</span>(<span>$html</span>->find(<span>'a'</span>) <span>as</span><span>$element</span>) 
       <span>echo</span><span>$element</span>->href . <span>'<br>'</span>;</span></span></code>
登录后复制

结果报错为:

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

这个报错:

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文件中可以看到这种错误日志:

<code><span>16</span> Log opened <span>at</span><span>2015</span>-<span>02</span>-<span>20</span><span>03</span>:<span>48</span>:<span>29</span><span>17</span> I: Connecting <span>to</span> configured address/port: localhost:<span>9000.</span><span>18</span> E: Could <span>not</span> connect <span>to</span> client. :-(
  <span>19</span> Log closed <span>at</span><span>2015</span>-<span>02</span>-<span>20</span><span>03</span>:<span>48</span>:<span>29</span></code>
登录后复制

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

<code>xdebug<span>.remote</span>_enable = on
xdebug<span>.remote</span>_handler = dbgp
xdebug<span>.remote</span>_host = localhost
xdebug<span>.remote</span>_port = <span>9000</span>
xdebug<span>.remote</span>_autostart = <span>1</span></code>
登录后复制

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

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

<code>xdebug.<span>remote_enable = on</span>
xdebug.<span>remote_handler = dbgp</span>
xdebug.<span>remote_host = localhost</span>
xdebug.<span>remote_port = 9000</span>
xdebug.<span>remote_autostart = 1</span>
;xdebug.<span>remote_log = /tmp/xdebug_remote.log</span></code>
登录后复制

加分号就ok了。

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!