百搜论坛欢迎您的加入!
adimg adimg
 
昨日:篇  今日:篇   总帖:篇   会员:
博主最大Lv63   
thinkphp自动采集怎么实现     

thinkphp实现自动采集功能的三种方法:

方法一:QueryList

个人感觉比较好用,采集详情比较不错的选择,但是采集复杂一点的列表,不好用。具体使用:

qq.png

控制器示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public function index(){

    // 使用采集类

    // 使用手册 :http://www.php.cn/php/php-QueryList3-ThinkPHP.html

    import('Org.QL.QueryList');

    $url = "http://www.zyctd.com/gqqg/";

    $reg = array();

    $reg['title'] = array('.sulist_title','text');

    $reg['shuliang'] = array('.su_li1','html');

    $obj = new \QueryList($url,$reg);

    $data = $obj->jsonArr;

    // foreach($data as $v){

    //     echo "<br>".$v['title'].'___'.$v['shuliang']."<br>";

    // }

    p($data);

}

相关推荐:《ThinkPHP教程

方法二:simple_html_dom

这个方法比较适合采集一点结构简单的页面,HTML标签的类名比较明确的页面,还不错。具体使用:

ww.png

控制器示例:

1

2

3

4

5

6

7

8

9

10

11

public function index(){

    // 参考文档:http://microphp.us/plugins/public/microphp_res/simple_html_dom/manual.htm#section_quickstart

    // 下载地址:https://github.com/samacs/simple_html_dom/edit/master/simple_html_dom.php

    // 使用方法:http://www.thinkphp.cn/topic/21635.html

    import("Org.Util.simple_html_dom", '', '.php');

    $html = file_get_html('http://www.zyctd.com/gqqg/');

    $ret = $html->find('.supply_list_box ul',0)->first_child();

    foreach($ret as $v){

        echo $v;

    };

}

方法三:获取页面HTMl,进行正则匹配采集

举例一个Demo:

采集一个页面:

http://www.zyctd.com/gqqg/

我要获取上面的四个信息:标题,数量,时间,跳转链接。

1566437997584782.png

获取这些信息,通过上面两种方法都采集不到,最后才选用的正则来采集。具体方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

public function index(){

    $url = "http://www.zyctd.com/gqqg/";

    // http://www.zyctd.com/gqqg-p1.html

    $supplyDB = M('supply');   

    $urlList = array();

    $array = array();

    for($x=1; $x<=1; $x++) {

        array_push($urlList,"http://www.zyctd.com/gqqg-p".$x.".html");

    };       

    foreach($urlList as $v){

        $curPageList = $this->getInfo($v);

        array_push($array,$curPageList);

    };

    foreach($array as $v){

        foreach($v as $vv){

            //echo $vv['title']."__".$vv['weight']."__".$vv['time']."<br>";

            $data = array();

            $data['title'] = $vv['title'];

            $data['weight'] = $vv['weight'];

            $data['add_time'] = $vv['add_time'];

            $data['url'] = $vv['url'];

            //$res = $supplyDB->add($data);

            //echo $res;

            echo "<p><span style='display:inline-block; width:110px;'>".$vv['title']."</span>

            <span style='display:inline-block; width:110px;'>".$vv['weight']."</span>

            <span style='display:inline-block; width:110px;'>".$vv['add_time']."</span>

            <span style='display:inline-block; width:110px;'>".$vv['url']."</span></p>";

        }

    }

        // 获取信息

        //$curPageList = $this->getInfo($html);

        //p($curPageList);

}

private function getInfo($url){

    $html = $this->getHtml($url);

    $array = array();

    // 匹配所有的标题

    preg_match_all("#<divclass=\"sulist_title\"><i></i><span>(.*?)</span></div>#",$html,$matches);

    $all_title = $matches[1];

    preg_match_all("#<i>发布时间:</i><span>(.*?)</span>#",$html,$matches);

    // 匹配所有的发布时间

    $all_time = $matches[1];

    // 匹配所有的求购数量

    preg_match_all("#<i>求购数量:</i><span>(.*?)</span>#",$html,$matches);

    $all_weight = $matches[1];

    // 匹配跳转链接

    preg_match_all("#<atarget=\"_blank\"href=\"(.*?)\">#",$html,$matches);

    $all_url = $matches[1];

    // 组合

    foreach($all_title as $k => $v){

        $arr = array();

        $arr['title'] = $v;

        $arr['weight'] = $all_weight[$k];

        $arr['add_time'] = $all_time[$k];

        $arr['url'] = $all_url[$k];

        array_push($array,$arr);

    }

    return $array;

}

private function getHtml($url){

    $html = file_get_contents($url);

    $html = preg_replace("#\n#","",$html);

    $html = preg_replace("#\r#","",$html);

    $html = preg_replace("#\\s#","",$html);

    return $html;

}

以上就是thinkphp自动采集怎么实现的详细内容

 0  已被阅读了1184次  楼主 2020-06-23 13:27:56
回复列表

回复:thinkphp自动采集怎么实现

联系站长 友链申请桂ICP备19000949号-1     桂ICP备19000949号-1
您的IP:18.189.22.136,2024-04-29 08:06:11,Processed in 0.03542 second(s).
免责声明: 本网不承担任何由内容提供商提供的信息所引起的争议和法律责任。
Powered by HadSky 7.12.9
已有0次打赏
(0) 分享
分享
取消
免责声明
1、本站资源,均来自网络,版权归原作者,所有资源和文章仅限用于学习和研究目的 。
2、不得用于商业或非法用途,否则,一切责任由该用户承担 !
如果觉得本文还不错请点个赞或者打赏点轻币哦~
拒绝伸手党,拿走请回复,尊重楼主,尊重你我他~

侵权删除请致信 E-Mail:207882320@qq.com