1. 程式人生 > >php抓取淘寶/天貓評論,抓取成功後自動儲存為"文字格式",儲存在當前目錄下

php抓取淘寶/天貓評論,抓取成功後自動儲存為"文字格式",儲存在當前目錄下

不說其他的,上段程式碼先

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 "儲存成功";
}
?>

抓取完成後: