Blogger Information
Blog 37
fans 0
comment 1
visits 29834
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
正则表达式:获取京东的 列表和详情-2019-10-21
H先生
Original
1192 people have browsed it

正则表达式:获取京东的 列表和详情

<?php

$url = 'https://search.jd.com/Search?keyword=冰箱&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=冰箱&stock=1&page=3&s=55&click=0';

$ret = curl($url);
$params = '/^/';
$preg = preg_match($params,$ret,$matches);
var_dump($preg);
$content = $matches[1].'</div>';
$content = $matches[1].'</div>';
// 把来源网站的图片标识,替换为HTML标识
$content = preg_replace('/data-original/','src',$content);
$content = preg_replace('/<div id="shortcut-2014">/','src',$content);
print_r($matches);
print_r($ret);


// 通过curl请求接口
function curl($url, $params = false, $ispost = 0)
{
    header('content-type: text/html; charset=utf-8');
    $httpInfo = array();
    $ch = curl_init();

    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包
        curl_setopt($ch, CURLOPT_URL, $url); // 设置URL
    } else {
        // GET请求,组装url
        if ($params) {
            $url = $url . '?';
            foreach ($params as $k => $v) {
                $url = $url . $k . '=' . $v . '&';
            }
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//爬取重定向页面
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);//自动设置Referer,防止盗链
    curl_setopt($ch, CURLOPT_HEADER, 0);//显示放回Header区域内容
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);//对证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 要求结果保存到字符串中还是输出到屏幕上
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // 默认值,让 cURL 自己判断使用哪个版本。 (强制使用 HTTP/1.1)。
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); // 在尝试连接时等待的秒数。设置为0,则无限等待。
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);  // 设置超时限制防止死循环


    curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData'); // 在HTTP请求中包含一个"User-Agent:"头的字符串
    $html = curl_exec($ch);   // 运行Curl,请求URL ,把结果复制给变量
    if (curl_error($ch)) {
        echo 'Error' . curl_error($curl);  // 捕抓异常
    }
    curl_close($ch);                    // 关闭cURL连接
    return $html;
}
?>

QQ截图20191021233123.pngQQ截图20191021233430.png

Correction status:qualified

Teacher's comments:学习可以, 数据不要用作其它用途
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post