• KingPaper教你写采集


    简单的几个函数介绍

    file_get _contents();

    preg_match();

     首先获取你要采集的网页 并采集内容

    $str = file_get_contents("www.baidu.com");// 获取地址url

    $regex = "/(<div class=\"content\" id=\"article\".*?><.*?>.*?<\/.*?><\/div>)/ism";//正则表达式 

    preg_match($regex,$str,$t);//正则匹配返回数组

    print_r($t);打印数组查看内容

     // 连接数据库

    mysql_connect(“localhost”,"root","root");

    mysql_select_db("test")

    mysql_query("insert into test(id,content)values('',$t[0])") 

    采集多条的话  将获取内容的正则封装成函数

    function getcontent($url){

    $str=file_get_contents($url);
    $regex = "/(<div class=\"content\" id=\"article\".*?><.*?>.*?<\/.*?><\/div>)/ism";
    preg_match($regex,$str, $t);
    return $t[0];

    然后将url放到数组内

    $str="www.baidu.com/1.html\n www.baidu.com/2.html \n www.baidu.com/3.html\nwww.baidu.com/5.html";

    $arr = explode("\n",$str); 

    foreach($arr as $val) {

    $content=getcontent($val);

    mysql_query("insert into test(id,content)values('',$content)");  

    欢迎光临我的网站 夕越网 http://www.xiyue369.com 

    既然选择了独立,就要在人群中独立出来,成为佼佼者。
  • 相关阅读:
    安装高版本的docker
    Apache JMeter汉化手册
    安装python包
    Jmeter Cluster
    doc下设置永久环境变量的好方法
    jmeter非常好的博客收藏
    mysql-学习链接
    python 脚本
    常见python快捷键
    2015年心情随笔--周围太烦躁,我想静静
  • 原文地址:https://www.cnblogs.com/kingpaper/p/3132431.html
Copyright © 2020-2023  润新知