利用爬虫在b站搜动漫找链接,jsoup实践

调用reZero方法输入要看的动漫即可,比如从零开始的异世界,fate,食戟之灵。。。

public static void reZero(String search) throws IOException {

if(search == null || "".equals(search)){
return;
}
System.out.println("丢你雷姆");

        String encoding = "utf-8";
        search = toBrowserCode(search, encoding);
        
String url = "http://search.bilibili.com/all?keyword="+search;
Document doc1 = Jsoup.connect(url).get();
Elements spans = doc1.select(".so-episode span");
List animeList = new ArrayList();
String anime = "";

for(Element span : spans){
if("TV".equals(span.html()) || span.html().contains("季") || "SP".equals(span.html())){
anime = span.parent().attr("href");
animeList.add(anime);
}

}

for(String a : animeList){
Document doc2 = Jsoup.connect(a).get();
String animeName = doc2.select(".b-head h1").get(0).text();
System.out.println(animeName);

Elements links = doc2.select(".slider-part a");
for(Element link : links){
String title = link.attr("title");
if(title.contains("第")){
System.out.println(link.attr("title")+":"+link.attr("href"));
}
}
}

}

public static String toBrowserCode(String word, String encoding)
            throws UnsupportedEncodingException
    {
        byte[] textByte = word.getBytes(encoding);
        StringBuilder strBuilder = new StringBuilder();
       
        for (int j = 0; j < textByte.length; j++)
        {
            // 转换为16进制字符
            String hexStr = Integer.toHexString(textByte[j] & 0xff);
            strBuilder.append("%" + hexStr.toUpperCase());
        }
       
        return strBuilder.toString();
    }

你可能感兴趣的:(利用爬虫在b站搜动漫找链接,jsoup实践)