>  기사  >  백엔드 개발  >  PHP+jQuery+POST采集网页示例

PHP+jQuery+POST采集网页示例

WBOY
WBOY원래의
2016-07-25 08:47:34969검색
利用JQuery强大的DOM操纵能力来采集页面数据,
然后组织数据以POST的方式发送数据给自身,
自身接收POST来的数据再以CSV格式写入到文件.

声明:
本程序仅作学习和演示之用,请勿频繁采集示例中的网址;
以免给目标网站造成不必要的麻烦!
欢迎大家提出意见
  1. set_time_limit(0);
  2. $num = range(0, 49100, 100);
  3. $base = 'http://www.zjchina.org/mspMajorIndexAction.fo?&startcount=';
  4. $page = isset($_GET['startcount']) ? $_GET['startcount'] : 0;
  5. $next_url = $_SERVER['SCRIPT_NAME'].'?startcount='.($page+1);
  6. if ( !isset($num[$page]) ) { exit('采集完了'); }
  7. //提交数据
  8. if ( $_POST && count($_POST) && isset($_POST['send']) ) {
  9. $send = $_POST['send'];
  10. $file = dirname(__FILE__).'/data.csv';
  11. if ( file_exists($file) ) { unset($send[0]); }
  12. $fp = fopen($file, 'a+');
  13. foreach($send as $line) { fputcsv($fp, $line); }
  14. fclose($fp);
  15. exit(json_encode(array('jump' => $next_url)));
  16. }
  17. //抓取数据
  18. $html = file_get_contents($base.$num[$page]);
  19. $html = str_replace('script', 'pre', $html);
  20. $html .= '
  21. ';
  22. echo $html;
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.