PHP数据采集

<?php

use QL\QueryList;

require_once './phpQuery.php';
require_once './QueryList.php';

//  连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=zfw', 'root', 'root');
// 创建一个范围内的数字,用于进行分页处理
$page = range(1, 3);
//  进行分页采集数据
foreach ($page as $value) {
    
    
    // 待采集的页面地址
    $url = 'https://news.ke.com/sh/baike/0035/pg' . $value . "/";
// 采集规则
    $rules = [
        // 文章标题
        'title' => ['.text .LOGCLICK', 'text'],
        //  概要
        'desn' => ['.summary', 'text'],
        //  封面图片
        'logo' => ['.lj-lazy', 'data-original', '', function ($item) {
    
    
            //   获取图片后缀
            $suffix = pathinfo($item, PATHINFO_EXTENSION);
            //  自定义文件名称
            $attr = md5($item) . time() . mt_rand(1, 9999999) . "." . $suffix;
            // 获取文件名称,保存图片到本地 生成本地路径地址
            $filepath = dirname(__DIR__) . "/public/admin/uploads/article/" . $attr;
            //  保存到本地
            file_put_contents($filepath, file_get_contents($item));
            return "/admin/uploads/article/" . $attr;
        }],
        //  采集 地址
        'url' => ['.text>.LOGCLICK', 'href']
    ];
    $data[] = @ QueryList::Query($url, $rules)->data;

    //  进行入库处理操作
    foreach ($data as $val){
    
    
        foreach ($val as $k=>$v){
    
    
//              sql处理
            $sql = "insert into zfw_articles (title,desn,logo,url,author,main) values (?,?,?,?,'','')";
            //  stmt对象处理
            $stmt = $pdo->prepare($sql);
            //  入库
            $stmt->execute([$v['title'],$v['desn'],$v['logo'],$v['url']]);
        }
    }
}






<?php
use QL\QueryList;

require_once './phpQuery.php';
require_once './QueryList.php';

//  使用PDO
$pdo = new PDO("mysql:host=127.0.0.1;dbname=zfw",'root','root');
//  查询内容页面
$sql = "select id,url from zfw_articles where main=''";
//  查询转数组
$row = $pdo->query($sql)->fetchAll(2);
//  循环抓取数据
foreach ($row as $value){
    
    
    $id = $value['id'];
    $ret =@ QueryList::Query($value['url'],[
        'author' => ['.author','text','',function($item){
    
    
            return substr($item,'9','6');
        }],
        'main' => ['.bd','html']
    ])->data;
    //  获取作者
    $author = $ret[0]['author'];
    //  获取内容
    $main = $ret[0]['main'];
        // 进行修改数据 入库
      $upsql = "update zfw_articles set author=?,main=? where id=? ";
      $stem = $pdo->prepare($upsql);
      //    入库
    $stem->execute([$author,$main,$id]);
}


猜你喜欢

转载自blog.csdn.net/zhouqi1427/article/details/113249480