求高手,類比瀏覽器抓取網頁

來源:互聯網
上載者:User
如抓取http://map.sogou.com/api/這個網頁,我寫的程式,如果不帶網址後面的"/",會抓取得不到,但是站上網(http://tool.chinaz.com/Tools/PageCode.aspx),不帶最後面的"/"即可抓取到(即:http://map.sogou.com/api),他是什麼原理?下面貼出My Code,請改進

function file_get($url){ ob_start(); $ch = curl_init();  curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie.txt"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; CIBA)");curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_exec($ch); curl_close($ch); $content = ob_get_clean();  return $content;}


回複討論(解決方案)

請加一句代碼:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

因為你抓取http://map.sogou.com/api,他返回的是301跳轉。加上這句就能抓取跳轉後的html了。

請加一句代碼:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

因為你抓取http://map.sogou.com/api,他返回的是301跳轉。加上這句就能抓取跳轉後的html了。
加上也不行,還是無法擷取


請加一句代碼:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

因為你抓取http://map.sogou.com/api,他返回的是301跳轉。加上這句就能抓取跳轉後的html了。
加上也不行,還是無法擷取


function file_get($url){ ob_start(); $ch = curl_init();  curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie.txt"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; CIBA)");curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,TRUE); curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_exec($ch); curl_close($ch); $content = ob_get_clean();  return $content;}


我測試過的,加上是行的呀。

CURLOPT_FOLLOWLOCATION

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.