不说其他的,上段代码先
index.html
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>抓取淘宝评论</title>
</head>
<style type="text/css">
.boss{
width:500px;
height:350px;
margin:0 auto;
margin-top: 10%;
}
img{
width:500px;
height:350px;
position: absolute;
z-index: -10;
}
.div1{
width: 200px;
height:200px;
margin:0 auto;
padding: 15px;
}
button{
width:180px;
height:30px;
background-color: skyblue;
margin-top: 10px;
}
#inp1{
width:115px;
}
</style>
<body>
<div class="boss">
<img src="1.jpg">
<div class="div1">
<h1>抓取淘宝评论</h1>
<form action="pinglun.php" method="post">
请填写itemId:<br><input type="text" id="inp2" placeholder="请填写itemId" name="itemId" value=""><br>
请填写spuId:<br><input type="text" id="inp2" placeholder="请填写spuId" name="spuId" value=""><br>
请填写sellerId:<br><input type="text" id="inp3" placeholder="请填写sellerId" name="sellerId" value=""><br>
请填写生成文件夹名:<br><input type="text" id="inp3" placeholder="XXX.txt格式" name="sed" value=""><br>
<button>提交</button>
</form>
</div>
</div>
</body>
</html>
pinglun.php
<?php
header("Content-Type:text/html;charset=utf-8");
$itemId = $_POST['itemId'];
$spuId = $_POST['spuId'];
$sellerId = $_POST['sellerId'];
$sed = $_POST['sed'];
if($itemId == ''){
echo "<script>alert('错误的itemId');history.go(-1);</script>";
}else if($spuId == ''){
echo "<script>alert('错误的spuId');history.go(-1);</script>";
}else if($sellerId == ''){
echo "<script>alert('错误的sellerId');history.go(-1);</script>";
}else if($sed == ''){
echo "<script>alert('错误的sed');history.go(-1);</script>";
}
$url="https://rate.tmall.com/list_detail_rate.htm?itemId=$itemId&spuId=$spuId&sellerId=$sellerId&order=3¤tPage=1&append=0&content=1&tagId=&posi=&picture=1";
$res = file_get_contents($url);
// 匹配评论部分
preg_match_all("/Content\":\"((.|\n)*?)\"/",$res, $match);
$wt = array();
$arr = $match[1];
// 去除空评论
foreach ($arr as $v) {
if($v != ''){
// 转化成utf-8编码
$wt[] = iconv("GBK","UTF-8", $v);
}
}
$str = implode("\r\n", $wt);
$txt = file_put_contents($sed, $str);
if($txt == 'false'){
echo "储存失败";
}else{
echo "储存成功";
}
?>
抓取完成后: