网页抓取接口基于phpQuery开发的一套网页爬虫接口,开发者可通过POST或者GET调用
API接口
前端调用需要把ip地址改为您服务器的域名,后端如是同一台服务器下可不用修改
接口地址:http://127.0.0.1/taosql/api/reptile.php
返回格式:json
请求方式:get/post
请求参数
| 参数 | 类型 | 是否必填 | 描述 | 示例值 |
|---|---|---|---|---|
| url | String | 是 | 爬取的网页地址 | https://www.baidu.com/ |
| jQuery | String | 是 | jQuery筛选元素 | body |
| attr | String | 是 | jQuery筛选属性 | html |
常用的元素及属性:
| 元素类型(jQuery) | 元素描述 | 属性(attr) | 描述 |
|---|---|---|---|
| a | 超链接 | href | 超链接地址 |
| text | 超链接文本 | ||
| html | 超链接HTML内容 | ||
| img | 图片 | src | 图片地址 |
| li | 列表 | text | 列表文本 |
| html | 列表HTML代码 | ||
| .class | 类选择器 | text | 元素内文本 |
| html | 元素内HTML代码 | ||
| title | 元素标题 | ||
| #id | id选择器 | text | 元素内文本 |
| html | 元素内HTML代码 | ||
| title | 元素标题 | ||
| …… | …… | …… | …… |
上面只列举了常用的元素,开发者请根据实际需求调整和设置获取的属性
http请求示例
http://127.0.0.1/taosql/api/reptile.php?url=https://www.baidu.com/&jQuery=img&attr=src
JavaScript请求示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>抓取网页案例</title>
<link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css" />
<script src="https://www.layuicdn.com/layui/layui.js"></script>
</head>
<body>
<script>
layui.use(function() {
var $ = layui.$;
$.ajax({
url: "http://127.0.0.1/taosql/api/reptile.php",
type: "GET",
dataType: "json",
data: {
url: "https://www.baidu.com/",
jQuery: "img",
attr: "src",
},
beforeSend: function() {
layer.msg("正在爬取", {
icon: 16,
shade: 0.05,
time: false
});
},
success: function(data) {
layer.msg(data.code, {
icon: data.icon
});
if (data.icon == "1") {
console.log(data);
alert("爬取网页成功");
}
},
error: function(data) {
var obj = eval(data);
layer.alert(obj.responseText, {
icon: 2
});
}
});
});
</script>
</body>
</html>
PHP请求示例
<?php
header("Content-type:text/html;charset=utf-8");
$url = "http://127.0.0.1/taosql/api/reptile.php";
$data = [
"url"=>"https://www.baidu.com",
"jQuery"=>"img",
"attr"=>"src"
];
$res = curl($url, $data);
exit($res);//$res是结果
function curl($u, $d=[], $h=[])
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $u);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
curl_setopt($curl, CURLOPT_HTTPHEADER, $h);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $d);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
$data = curl_exec($curl);
if (curl_error($curl)) {
exit("无法发起POST请求");
}
curl_close($curl);
return $data;
}
?>
响应参数
| 参数 | 类型 | 描述 | 状态码 | 状态码说明 |
|---|---|---|---|---|
| icon | String | 状态码 | 1 | 网页爬取成功 |
| 5 | 参数不全或者有误 | |||
| code | String | 文本描述 | - | - |
| count | Number | 元素的数量 | - | - |
| data | Array | 爬取的结果 | - | - |
响应示例
{
"code": "调试成功",
"icon": "1",
"count": 4,
"data": [
"images/1.png",
"images/2.png",
"images/3.png",
"images/4.png",
]
}
