|
楼主 |
发表于 2017-7-22 23:53
|
显示全部楼层
本帖最后由 Missde 于 2017-7-23 13:19 编辑
代码1:
先抓取对方的HTML,然后分析页面中的种子
- <?php
- date_default_timezone_set('Asia/Shanghai');
- header('Content-type: text/html; charset=utf-8');
- define('CACHE', "cache/");
- if(!file_exists(CACHE))mkdir(CACHE,0777);
- echo '
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="apple-mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- <meta name="format-detection" content="telephone=no">
- <style type="text/css">
- body{background:#f9f9f9;}
- body,ul,li{margin:0;padding:0;list-style-type:none;}
- li{position:relative;padding:.7em 15px;border-bottom:1px solid #ccc;background-image:linear-gradient(#fff,#eee)}
- li a{font-size:1rem;color:#2f3e46;text-decoration:none;}
- li:after{position:absolute;right:3px;top:1rem;font-size:1rem;content:">";display:inline-block;width:20px;height:20px;line-height:20px;text-align:center;background:#222;color:#ddd;border-radius:50%;}
- div{padding:20px;background-image:linear-gradient(#f9f9f9,#eee);word-wrap:break-word ;}
- </style>
- </head>
- <body>
- ';
- btbtt();
- function btbtt(){
- $domain="http://www.btbtt.net/";
- if($torrent=get("torrent")){
- $url=$domain.$torrent;
- $html=read($url);
- $name=md5($url).'.torrent';
- if(stripos($_SERVER['HTTP_HOST'],':81')!==false)$folder='/mnt/transmission/watch/';
- else $folder='';
- file_put_contents($folder.$name,$html);
- echo '<center style="color:#f00"><br><br> 导入种子 ok </center>';
- exit();
- }elseif($page=get("page")){
- $html=read($domain.$page);
- $A=dom($html);
- echo "<div>网址:<a href='".$domain.$page."' target='_blank'>".$domain.$page."</a></div><div>内容:";
- //主题内容提取
- $html=substring($html,'post_td','</td>');
- $html=preg_replace("/<br.[^\>]*>/s","\n",$html);
- $html=str_replace(array("<p>","\r",'" valign="top">'),"\n",$html);
- $html=strip_tags($html,'<a>');
- $html=preg_replace("/\n\s+|BAIDU_CLB_SLOT_ID = "\d+";|\-\-\>|\#1楼/s","\n",$html);
- $html=preg_replace("/\n+/s","<br>",$html);
- $html=str_replace("网盘下载:","<font color=red>网盘下载:</font>",$html);
- echo $html."</div>";
- foreach($A as $r){
- $url=$r['url'];
- if(stripos($url,'attach-dialog')!==false){
- $downurl=str_replace("dialog",'download',$url);
- $file=CACHE.md5(strtolower($domain.$downurl));
- $tishi=file_exists($file)?'你已经导入过,再次':'';
- echo "<li><font color=red>".$tishi."点击这里导入种子:</font><a href='?torrent=".$downurl."'>".$r['html']."</a>";
- }
- }
- exit();
- }
- //列表页
- $page=get("pid");
- if($page<1)$page=1;
- $html=read($domain."forum-index-fid-951-page-".$page.".htm",36000);
- $A=dom($html);
- echo "<ul>";
- foreach($A as $r){
- $url=$r['url'];
- if(stripos($url,'thread-index-fid-')===false
- || stripos($url,'fid-2-')!==false
- || strlen($r['html'])<10
- || stripos($r['css'],'thread-digest')!==false
- )continue;
- echo '<li><a href="?page='.$url.'">'.$r['title']."</a></li>";
- }
- echo "</ul><div>";
- for($i=1;$i<=10;$i++)echo "<a href='?pid=".$i."'>P".$i."</a> - ";
- echo "</div>";
- }
- function curl($url){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- curl_close($curl);
- if(strlen($data)<3000)die("cURL - Error - ".$url);
- return $data;
- }
- function read($url,$timeout=8640000){
- $file=CACHE.md5(strtolower($url));
- if(file_exists($file) && $timeout){
- if((time()-filemtime($file))<$timeout){
- return file_get_contents($file);
- exit();
- }
- }
- $html=curl($url);
- file_put_contents($file,$html);
- return $html;
- }
- function dom($html=''){
- if(!$html)die("DOM Error");
- $dom = new DOMdocument();
- @$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'.$html);
- $A=$dom->getElementsByTagName("a");
- $lis=array();
- foreach($A as $a){
- $lis[]=array(
- 'url'=>$a->getAttribute("href"),
- 'title'=>$a->getAttribute("title"),
- 'html'=>$a->nodeValue,
- 'css'=>$a->getAttribute("class"),
- );
- }
- return $lis;
- }
- function substring($msg,$a,$b,$c=0){
- $i=$a==""?0:intval(stripos($msg,$a));
- if($i<1)return "";
- if($c<1)$i+=strlen($a);
- $msg=substr($msg,$i);
- $j=$b==""?strlen($msg):stripos($msg,$b);
- $msg=substr($msg,0,$j);
- return $msg;
- }
- function get($k){
- $str=isset($_GET[$k])?$_GET[$k]:"";
- if(stripos($k,'id')!==false)$str=intval($str);
- $str=str_replace(array('"',"'",'?','<','>',"\r","\n","\"),'',$str);
- return trim($str);
- }
复制代码
将上面代码保存成 utf-8格式的文件,比如bt.php 放在你的路由器的web php 环境中,访问http://192.168.123.1/bt.php就可以通过手机下载这家网站的BT种子。
代码2:
如果你不像抓取种子网站的HTML,而是去他们网站浏览,复制种子的URL,
页面可以改成一个表单,抓取对方的http://*****.torrent ,并保存到 transmission/watch/ 更简单些。
- <?php
- date_default_timezone_set('Asia/Shanghai');
- header('Content-type: text/html; charset=utf-8');
- define('CACHE', "cache/");
- if(!file_exists(CACHE))mkdir(CACHE,0777);
- echo '
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="apple-mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- <meta name="format-detection" content="telephone=no">
- <style type="text/css">
- body{background:#f9f9f9;}
- body,ul,li{margin:0;padding:0;list-style-type:none;}
- li{position:relative;padding:.7em 15px;border-bottom:1px solid #ccc;background-image:linear-gradient(#fff,#eee)}
- li a{font-size:1rem;color:#2f3e46;text-decoration:none;}
- li:after{position:absolute;right:3px;top:1rem;font-size:1rem;content:">";display:inline-block;width:20px;height:20px;line-height:20px;text-align:center;background:#222;color:#ddd;border-radius:50%;}
- div{padding:20px;background-image:linear-gradient(#f9f9f9,#eee);word-wrap:break-word ;}
- </style>
- </head>
- <body>
- <form action="?" method="get">
- <input type="text" name="torrent"/>
- <input type="submit" value="导入种子URL"/>
- </form>
- ';
- $url=@$_GET["torrent"];
- if(stripos($url,'http:')===0 || stripos($url,'https:')===0){
- $html=read($url);
- $file=md5($url).'.torrent';
- if(stripos($_SERVER['HTTP_HOST'],':81')!==false)$file='/mnt/transmission/watch/'.$file;
- if(file_exists($file) || file_exists($file.'.added')){
- die('种子已经在下载中,请先通过Transmission删除旧的再导入');
- }else{
- file_put_contents($file,$html);
- die('导入种子 ok');
- }
- }
- exit();
- function read($url,$timeout=8640000){
- $file=CACHE.md5(strtolower($url));
- if(file_exists($file) && $timeout){
- if((time()-filemtime($file))<$timeout){
- return file_get_contents($file);
- exit();
- }
- }
- $html=curl($url);
- if($html){
- file_put_contents($file,$html);
- return $html;
- }
- die("抓取数据失败");
- }
- function curl($url){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- curl_close($curl);
- return $data;
- }
复制代码
本例写的匆忙,不求完美,抛砖引玉。
|
|