首頁 >後端開發 >php教程 >使用php-simple-html-dom-parser遇到的問題

使用php-simple-html-dom-parser遇到的問題

WBOY
WBOY原創
2016-08-08 09:29:361572瀏覽

今天使用這個工具:

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>

這個報錯:

host

create_debugger_socket(“localhost” ) 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 xdem.remote_bug.remof /php.ini中為xdebug的配置加入了
xdebug.remote_log = /tmp/xdebug_remote.log

之後我們繼續運行,繼續出錯,不過我們可以到/tmp/xdebug_remote.錯誤日誌:

<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教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn