利用jsoup爬取百度网盘资源分享连接(多线程)
突然有一天就想说能不能用某种方法把百度网盘上分享的资源连接抓取下来,于是就动手了。知乎上有人说过最好的方法就是http://pan.baidu.com/wap抓取,一看果然链接后面的uk值是一串数字,就想到能够自己拼装链接,循环不断的去抽取页面。于是自己分析了下页面结构,就開始了
从一開始写的时候,发现一秒钟就抽取了一个页面,想到之前用的webmagic爬虫里抓取页面就用了java的多线程技术,于是百度。。。直接上代码。(抓取过程中发现好多无效资源,垃圾资源,广告资源特别多,所以慢慢的累积了非常多垃圾keyword,在抓取过程中过滤)
package getBaiduYunURL; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; class mythread implements Runnable{ long count; public void run() { try{ for(;this.count<1813454114;this.count++){ PrintWriter printWriter = new PrintWriter(new FileWriter(new File("D:/自学/BaiduYunData/Data/url.txt"), true)); PrintWriter cachePrintWriter = new PrintWriter(new FileWriter(new File("D:/自学/BaiduYunData/Cache/index.txt"), false)); cachePrintWriter.println(this.count); cachePrintWriter.close(); String url="http://pan.baidu.com/wap/share/home?uk="+this.count; Document doc = Jsoup.connect(url).timeout(60000).get(); String title = doc.title(); System.out.println(title+this.count); Element content = doc.body(); Elements emptytag=content.select(".empty-other"); //看是否有分享 为空则是有分享 if(emptytag.isEmpty()){ System.out.println("有分享"); Elements dataems=content.select("[data-ac=active]"); for(Element dataem:dataems){ Elements lists=dataem.select(".list-item"); String sourcename=dataem.attr("data-fn"); if(sourcename!=""){ if(!sourcename.matches("^\\w+.[^sS]+$|^\\w+[-]\\w+.[^sS]+$|^\\w+[.?!;]\\w+.[^Ss]+$|\\w+|^.*[!??!].*$")){ System.out.println("不是数字"); if(sourcename.indexOf("医院")==-1&&sourcename.indexOf("淘宝")==-1 &&sourcename.indexOf("彩票")==-1&&sourcename.indexOf("福彩")==-1&&sourcename.indexOf("牌")==-1 &&sourcename.indexOf("双色球")==-1&&sourcename.indexOf("创业")==-1&&sourcename.indexOf("咨询")==-1 &&sourcename.indexOf("赚")==-1&&sourcename.indexOf("网店")==-1 &&sourcename.indexOf("营销")==-1&&sourcename.indexOf("娱乐")==-1 &&sourcename.indexOf("cf刷枪")==-1&&sourcename.indexOf("哪里")==-1 &&sourcename.indexOf("麻将")==-1&&sourcename.indexOf("作弊")==-1 &&sourcename.indexOf("早泄")==-1&&sourcename.indexOf("人流")==-1 &&sourcename.indexOf("包皮")==-1&&sourcename.indexOf("痔")==-1 &&sourcename.indexOf("肾")==-1&&sourcename.indexOf("治疗")==-1&&sourcename.indexOf("病")==-1 &&sourcename.indexOf("哪家")==-1&&sourcename.indexOf("哪个")==-1 &&sourcename.indexOf("妇科")==-1&&sourcename.indexOf("男科")==-1 &&sourcename.indexOf("复件")==-1&&sourcename.indexOf("痘")==-1 &&sourcename.indexOf("免费")==-1&&sourcename.indexOf("qq")==-1 &&sourcename.indexOf("QQ")==-1&&sourcename.indexOf("减肥")==-1 &&sourcename.indexOf("高考志愿")==-1&&sourcename.indexOf("瘦身")==-1 &&sourcename.indexOf("新建")==-1&&sourcename.indexOf("挂")==-1 &&sourcename.indexOf("解压")==-1&&sourcename.indexOf("肝")==-1&&sourcename.indexOf("炎")==-1 &&sourcename.indexOf("补丁")==-1&&sourcename.indexOf("疤痕")==-1&&sourcename.indexOf(".exe")==-1 &&sourcename.indexOf("刷")==-1&&sourcename.indexOf(".com")==-1&&sourcename.indexOf("美女")==-1){ System.out.println(sourcename); printWriter.println(sourcename); for(Element listem:lists){ String linkHref = url+listem.attr("href"); printWriter.println(linkHref); System.out.println(linkHref); } }else{ System.out.println(sourcename); } }else{ System.out.println("是数字"); } } } } printWriter.close(); cachePrintWriter.close(); } }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public class MultiThread { public static void main(String args[]) { mythread my = new mythread(); try{ FileReader text = new FileReader("D:/自学/BaiduYunData/Cache/index.txt"); BufferedReader buff = new BufferedReader(text); String index=buff.readLine(); my.count=Long.parseLong(index); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(int i=0;i<100;i++){ new Thread(my).start(); } } }代码如上,非常easy,我是把抓取的链接放在txt中,下一步就是把这些资源做成能够在线搜索,和非常多网盘搜索一样,可是我这个比較低级。