可否帮忙写一个单页页的PHP采集程序,并附上实例
比方说,我要采集这个页面:http://news.163.com/12/0613/20/83TJ7PA700014JB6.html
要求:
采集标题
采集正文
谢谢!
[解决办法]
首先去http://simplehtmldom.sourceforge.net/index.htm(点击Download latest version form Sourceforge.)下载一个simple_html_dom.php,傻瓜式的正则,另官网上有详细教程,很容易看懂。
header("Content-type: text/html; charset=gb2312");
require dirname(__FILE__) . '/simple_html_dom.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://news.163.com/12/0613/20/83TJ7PA700014JB6.html');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$htmls = curl_exec($ch);
curl_close($ch);
$html = str_get_html($htmls);
foreach($html->find('#h1title') as $title){
echo strip_tags($title).'<br />';//标题
}
foreach($html->find('#endText') as $content){
echo strip_tags($content);//正文
}
注:curl模拟Mozilla浏览器抓取,比file_get_contents()解析速度更快。strip_tags() 用以去除那个邪恶的iframe广告,反正你需要的是“采集标题”和“采集正文”冗余的html标签对你无用。
[解决办法]
PHP获取QQ邮箱好友列表的方法:
本文为大家介绍有关如何运用PHP获取QQ邮箱好友的方法。PHP有一个扩展是curl扩展,该扩展一般用于采集数据。但是也可以实现模拟登陆,通过模拟登陆来登陆QQ邮箱,再利用curl的采集来获取邮箱的好友列表。以上是实现原理,理解袁莉以后我们来看实现过程。具体的PHP获取QQ邮箱好友的代码如下:
1.<?php
2.class QQHttp {
3. var $cookie = '';
4. function __cunstrut() {
5. }
6. function makeForm() {
7. $form = array(
8. 'url' => "http://mail.qq.com/cgi-bin/loginpage",
9. );
10. $data = $this->curlFunc($form);
11. preg_match('/name="ts"svalue="(d+)"/',$data['html'], $tspre);
12. $ts = $tspre[1];
13. preg_match('/action="http://(md+).mail.qq.com/',$data['html'], $server);
14. $server_no = $server[1];
15. /* login.html 载入 */
16. $html = file_get_contents(dirname(__FILE__).'/login.htm');
17. $html = str_replace('{_ts_}',$ts, $html);
18. $html = str_replace('{_server_no_}',$server_no, $html);
19. return $html;
20. }
21. function curlFunc($array)
22. {
23. $ch = curl_init();
24. curl_setopt($ch, CURLOPT_URL, $array['url']);
25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
26. if( isset($array['header']) && $array['header'] ) {
27. curl_setopt($ch, CURLOPT_HEADER, 1);
28. }
29. if(isset($array['httpheader'])) {
30. curl_setopt($ch, CURLOPT_HTTPHEADER, $array['httpheader']);
31. }
32. if(isset($array['referer'])) {
33. curl_setopt($ch, CURLOPT_REFERER, $array['referer']);
34. }
35. if( isset($array['post']) ) {
36. curl_setopt($ch, CURLOPT_POST, 1 );
37. curl_setopt($ch, CURLOPT_POSTFIELDS, $array['post']);
38. }
39. if( isset($array['cookie']) ){
40. curl_setopt($ch, CURLOPT_COOKIE, $array['cookie']);
41. }
42. $r['erro'] = curl_error($ch);
43. $r['errno'] = curl_errno($ch);
44. $r['html'] = curl_exec($ch);
45. $r['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
46. curl_close($ch);
47. return $r;
48. }
49. /**
50. * 获取验证码图片和cookie
51. * @param Null
52. * @return array('img'=>String, 'cookie'=>String)
53. */
54. function getVFCode ()
55. {
56. $vfcode = array(
57. 'header' => true,
58. 'cookie' => false,
59. 'url'=>'http://ptlogin2.qq.com/getimage?aid='.$_GET['aid'].'&'.@$_GET['t'],
60. );
61. $r = $this->curlFunc($vfcode);
原文:http://www.phpnewer.com/index.php/Tszj/detail/id/436.html
[解决办法]
用抓取页面就可以,标题的话就是title标签之间的,正文是body之间的,用正则去掉一些不需要的内容