想通過本地代理,抓取遠程網頁內容,代碼如下:
array( 'proxy'=>'tcp://192.168.1.108:8087', 'request_fulluri '=>true, "method" => "GET", "timeout" => 2, ),);$context = stream_context_create($options);$fp = stream_socket_client("tcp://www.bigxu.com:80", $errno, $errstr, 30,STREAM_CLIENT_CONNECT,$context);// print_r(stream_context_get_options($fp)); exit;if (!$fp) { echo "$errstr ($errno)
\n";} else { fwrite($fp, "GET / HTTP/1.0\r\nHost: www.bigxu.com\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp);}?>
php file.php
nginx訪問日誌是:
15.196.206.102 [26/Apr/2014:12:04:45 +0800] http://www.bigxu.com/ 200 20630 0.241 "-" "-"
15.196.206.102 是我的本機IP.
$context沒有起到作用。
代理是絕對可以用的。
因為通過下面代碼,$context會起作用
$options = array( 'http'=>array( 'proxy'=>'tcp://192.168.1.108:8087', 'request_fulluri '=>true, "method" => "GET", "timeout" => 2, ), ); $context = stream_context_create($options); if ( $fp = fopen("http://www.bigxu.com", 'r', false, $context) ) { print "well done"; while (!feof($fp)) { echo fgets($fp, 1024); } }
php file.php
bigxu.com nginx訪問日誌是:
8.35.201.32 [26/Apr/2014:12:03:03 +0800] http://www.bigxu.com/ 200 7070 0.122 "-" "AppEngine-Google; (+http://code.google.com/appengine; appid: s~goagent0527)"
8.35.201.32 是我的代理IP
比較大型的爬蟲項目,我肯定會用stream_socket_client來串連,大家幫我看一下,這個函數,我怎麼用錯了呢?
回複內容:
想通過本地代理,抓取遠程網頁內容,代碼如下:
array( 'proxy'=>'tcp://192.168.1.108:8087', 'request_fulluri '=>true, "method" => "GET", "timeout" => 2, ),);$context = stream_context_create($options);$fp = stream_socket_client("tcp://www.bigxu.com:80", $errno, $errstr, 30,STREAM_CLIENT_CONNECT,$context);// print_r(stream_context_get_options($fp)); exit;if (!$fp) { echo "$errstr ($errno)
\n";} else { fwrite($fp, "GET / HTTP/1.0\r\nHost: www.bigxu.com\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp);}?>
php file.php
nginx訪問日誌是:
15.196.206.102 [26/Apr/2014:12:04:45 +0800] http://www.bigxu.com/ 200 20630 0.241 "-" "-"
15.196.206.102 是我的本機IP.
$context沒有起到作用。
代理是絕對可以用的。
因為通過下面代碼,$context會起作用
$options = array( 'http'=>array( 'proxy'=>'tcp://192.168.1.108:8087', 'request_fulluri '=>true, "method" => "GET", "timeout" => 2, ), ); $context = stream_context_create($options); if ( $fp = fopen("http://www.bigxu.com", 'r', false, $context) ) { print "well done"; while (!feof($fp)) { echo fgets($fp, 1024); } }
php file.php
bigxu.com nginx訪問日誌是:
8.35.201.32 [26/Apr/2014:12:03:03 +0800] http://www.bigxu.com/ 200 7070 0.122 "-" "AppEngine-Google; (+http://code.google.com/appengine; appid: s~goagent0527)"
8.35.201.32 是我的代理IP
比較大型的爬蟲項目,我肯定會用stream_socket_client來串連,大家幫我看一下,這個函數,我怎麼用錯了呢?
解決了,很難想像,我居然在一個日本的網站找到了答案,所以不要隨意抵制日本知識,哈哈!經過測試完全可以實現,你看看:
原文地址:http://pe5974.sakura.ne.jp/contents/proxy-https.php
說到採集,首先應該想到的是php的curl函數
最好的辦法就是類比爬蟲(比如:百度蜘蛛爬蟲或者google蜘蛛爬蟲),同樣也支援代理配置
通過爬蟲類比瀏覽器的head請求,沒有什麼抓去不到(理論上只要能通過瀏覽器請求到的資料,不管是要登入還是不要登入都是可以抓去到內容的)
不確定是不是這個代理有問題,配置個自己的代理看一下?
或者跟進一下? stream_socket_client 空了繼續弄吧