要提取google搜索的部分數據,發現google對於軟件抓取它的數據屏蔽的厲害,以前偽造下 USER-AGENT 就可以抓數據,但是現在卻不行了。利用抓包數據發現,Google 判斷了 cookies,當你沒有cookies的時候,直接返回 302 跳轉,而且是連續幾十個302跳轉,根本抓不了數據。
因此,在發送搜索命令時,需要先提取 cookies 並保存,然後利用保存下來的這個cookies再次發送搜索命令即可正常抓數據了。這其實和論壇的模擬登錄一個道理,先POST登錄,獲取cookies並保存,然後利用這個cookies訪問就可以了。
$cookie_jar = dirname(__FILE__)."/pic.cookie";
$url = "http://1.2.3.4/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); $content = curl_exec($ch); curl_close($ch);
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305'); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $ret = curl_exec($ch); curl_close($ch);
$post = "name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/loginstudent.action"); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); $result=curl_exec($ch); curl_close($ch);
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/accountcardUser.action"); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); $html=curl_exec($ch); // var_dump($html); curl_close($ch);