想获取一个网站的网站内容,但总是获取不到该怎么办
Jun 06, 2016 pm 08:26 PM
是这个网站:http://www.reg007.com/search。
比如说,我在输入框里面输入了981267080qq.com
他就会跳转到http://www.reg007.com/search?q=981267080-at-qq.com。
我想用php的file_get_contents获取http://www.reg007.com/search?q=981267080-at-qq.com的网页内容,但是总获取不到。
我应该怎么去做?
不明白他是怎么做的。是判断我的IP还是怎么弄的。
回复内容:
是这个网站:http://www.reg007.com/search。
比如说,我在输入框里面输入了981267080qq.com
他就会跳转到http://www.reg007.com/search?q=981267080-at-qq.com。
我想用php的file_get_contents获取http://www.reg007.com/search?q=981267080-at-qq.com的网页内容,但是总获取不到。
我应该怎么去做?
不明白他是怎么做的。是判断我的IP还是怎么弄的。
应该是请求的时候, 没带Cookie, 没带 Referer
.
其次真正的搜索是通过 Ajax 进行的, 即你请求的URL地址还少一部分内容.
运行结果:
代码:
<?php function request($url, $ck = '', $referer = '', $data = array()){ $headers = array( 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36' ); $method = 'GET'; if(count($data) > 0){ $method = 'POST'; $headers[] = 'X-Requested-With: XMLHttpRequest'; $headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'; } //如果有传递 Cookie if($ck != ''){ $headers[] = 'Cookie: ' . $ck; } //如果有传递 Referer if($referer != ''){ $headers[] = 'Referer: ' . $referer; } $opts = array( 'http' => array( 'method'=> $method, 'header'=> implode("\r\n", $headers) ) ); if(count($data) > 0){ $opts['http']['content'] = http_build_query ($data); } $context = stream_context_create($opts); $html = file_get_contents($url, false, $context); return array( $html,//本次请求得到的HTML $http_response_header//本次请求服务器返回的响应头 ); } //先请求一次, 从响应头中获取 Cookie $data = request('http://www.reg007.com/'); $headers = implode("", $data[1]); preg_match_all('/Set-Cookie: (.+?;)/', $headers, $session); if(count($session) !== 2){ die('获取Cookie失败!'); } $ck = implode(' ', $session[1]);//得到Cookie $data = request('http://www.reg007.com/search?q=981267080-at-qq.com', $ck, 'http://www.reg007.com/'); $html = $data[0];//取出来 HTML preg_match('/var h="(.+?)"/', $html, $h); if(count($h) !== 2){ die('获取Ajax请求Token失败!'); } $h = $h[1]; $ck .= ' q=' . urlencode('981267080@qq.com'); //这个查询比较耗时, 会有点慢 $data = request( 'http://www.reg007.com/search/ajax', $ck, 'http://www.reg007.com/', array( 'q'=>'981267080@qq.com', 'h'=>$h, 'i'=>0, 't'=>0 ) ); $result = json_decode($data[0]); var_dump($result);
那个网站显示的结果, 会发多个 ajax 去查, 上面的代码中只发一个, 其他的请楼主自己完成.

人気の記事

人気の記事

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法
