小偷程序(PHP 搜狗)

<?php
if(empty($_GET['key'])){
    echo '請輸入關鍵詞,域名上加?key=';exit;
}

/**************************獲取搜搜內容***********************************/
$key = $_GET['key'];
mb_convert_encoding($key,'utf-8','gb2312');
$url = 'https://www.sogou.com/sogou?query='. urlencode($key) .'&insite=wenwen.sogou.com';
//$url = 'https://www.sogou.com/sogou?query=香花槐&_ast=1574942592&_asf=www.sogou.com&w=01029901&pid=sogou-wsse-a9e18cb5dd9d3ab4&duppid=1&cid=&s_from=result_up&insite=wenwen.sogou.com' ;
$content = httpRequest($url);
mb_convert_encoding($content,'utf-8','gb2312');
$str = $content;
$str = str_replace('href="/css/', "https://www.sogou.com/css/", $content);
$str = str_replace('src="/images/', "https://www.sogou.com/images/", $str);
$str = str_replace('src="/js/', "https://www.sogou.com/js", $str);
$str = str_replace('href="/web/css/', "https://www.sogou.com/web/css/", $str);
$str = str_replace('src="/web/images/', "https://www.sogou.com/web/images/", $str);
$str = str_replace('src="/web/js/', "https://www.sogou.com/web/js/", $str);
$str = str_replace('src="/web/img/', "https://www.sogou.com/web/img/", $str);

//替換內容
$search = '/<div class="header" style="">.*?<\/p>(\t|\r\n|\n|\s)*<\/div>/is';
$str = preg_replace($search, "", $str);
$str = preg_replace('/搜狗搜索/', "", $str);
$str = preg_replace('/搜狗/', "", $str);
$str = preg_replace('/站內搜索/', "", $str);
$str = preg_replace('/全網搜索/', "", $str);
$str = preg_replace('/- 問問/', "", $str);
$str = preg_replace('/最佳答案/', "用戶評價", $str);
$search = '/<div class="biz_fb">.*?<\/a>(\t|\r\n|\n|\s)*<\/div>/is';
$str = preg_replace($search, "", $str);

$search="/<p class=\"str_time\".*?>.*?<\/p>/ism";
$str = preg_replace($search, "", $str);

$search="/<p class=\"num-tips\".*?>.*?<\/p>/ism";
$str = preg_replace($search, "", $str);

$search = '/<div class="p" id="pagebar_container">.*?<\/a>(\t|\r\n|\n|\s)*<\/div>/is';
$str = preg_replace($search, "", $str);

$search = '/<div class="cr" id="s_footer">.*?<\/div>(\t|\r\n|\n|\s)*<\/div>/is';
$str = preg_replace($search, "", $str);

$search="/<div class=\"fb\".*?>.*?<\/div>/ism";
$str = preg_replace($search, "", $str);

$search="/<div class=\"biz_risk_alert\".*?>.*?<\/div>/ism";
$str = preg_replace($search, "", $str);


//替換鏈接
$str = preg_replace('/<a .*?href="(.*?)".*?>/is',"<a href='http://www.cctv.com'>",$str);


echo $str;

echo '
 <style>
 .wrapper {
    padding: 0;
}
</style>
';

/**************************獲取關鍵詞***********************************/

$url = 'https://www.sogou.com/tx?query='. urlencode($key);

$content = httpRequest($url);
mb_convert_encoding($content,'utf-8','gb2312');

//構造一個div
echo '<div style="padding-left:38em;" class="content_re">';
echo '<h2>相關搜索</h2>';
//1
$search="/<div class=\"hint-mid\">.*?<\/a><\/div>/ism";
//$str2 = preg_replace($search, "", $content);
$str2 = preg_match($search, $content, $res);
$html=preg_replace("(<a[^>]*>(.+?)<\/a>)","<a href='/test.php?key=$1'>$1</a>",$res[0]);
echo $html;


//3
$search = '/<div class="hintBox">.*?<\/table>(\t|\r\n|\n|\s)*<\/div>/is';
$str = preg_match($search, $content, $div);

$html = $div[0];
/*$html = preg_replace('/<a .*?href="(.*?)".*?>/is',"<a href='http://www.cctv.com'>",$html);*/

$html=preg_replace("(<a[^>]*>(.+?)<\/a>)","<a href='/test.php?key=$1'>$1</a>",$html);
$html=preg_replace("/相關搜索/","",$html);
echo $html;

//2
$search="/<p>(\t|\r\n|\n|\s)<a.*?<\/p>/ism";
//$str2 = preg_replace($search, "", $content);
$str2 = preg_match($search, $content,$res);
//替換鏈接+關鍵詞(test.php 根據自己的改)
$html=preg_replace("(<a[^>]*>(.+?)<\/a>)","<a href='/test.php?key=$1'>$1</a>",$res[0]);
echo $html;

echo '</div>';



//樣式寫在這
echo '
  <style>
  .hint-mid {
    font-size: 0;
    line-height: 0;
    max-height: 102px;
    overflow: hidden;
    margin-bottom: 0;
    padding: 5px 0;
}
.hint-mid a {
    display: inline-block;
    font-size: 13px;
    line-height: 28px;
    height: 28px;
    padding: 0 7px;
    margin-right: 8px;
    text-decoration: none;
    border: 1px solid #e6e6e6;
    margin-bottom: 8px;
    border-radius: 1px;
}
.content_re{

}
p a {
    white-space: nowrap;
    padding: 4px 8px;
}
.hintBox{
margin-left: 0;
}
</style>
';


//curl 抓取內容
function httpRequest($url, $postData = array())
{
    //curl 僞造useragent
    $useragent = array(
        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)',
        'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)',
        'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
        'Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1',
        'Opera/9.27 (Windows NT 5.2; U; zh-cn)',
        'Opera/8.0 (Macintosh; PPC Mac OS X; U; en)',
        'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13 ',
        'Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    if (!empty($postData)) {
        // 設置請求方式爲post
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_ENCODING , 'gzip');

    //添加這兩行
//    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
//    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Origin: http://h5.eqxiu.com']);

    // 設置iP和useragent
    curl_setopt($ch, CURLOPT_REFERER, "http://h5.eqxiu.com/s/Rwg9jFlv");
    curl_setopt($ch, CURLOPT_USERAGENT, array_rand($useragent));

    //函數中加入下面這條語句

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    // (3)執行
    $result = curl_exec($ch);
    // (4)關閉
    curl_close($ch);
    return $result;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章