java动漫_使用Java获取一个动漫网站所有的动漫名

本文介绍了如何使用Java通过HTTP请求从动漫网站抓取动漫名称,包括Link类的构造、Html类的网页加载和解析,以及Anime类中获取动漫名列表的过程。通过示例展示了从第1到416页获取所有动漫名的完整步骤。
摘要由CSDN通过智能技术生成

使用Java获取一个动漫网站所有的动漫名

public class Link

{

private String frontStr;

private String behindStr;

public String linkStr;

public Link()

{

frontStr = "http://www.imomoe.ai/so.asp?page=";

behindStr = "&dq=%C8%D5%B1%BE&pl=time";

}

public String getPageLink(int page)

{

String temp = frontStr + (String.valueOf(page)) + behindStr;

return temp;

}

}

然后创建一个Html类:

public class Html

{

private String htmlStr;

public Html()

{

htmlStr = null;

}

public void loadHtml(int page) throws Exception

{

Link link = new Link();

String linkStr = link.getPageLink(page);

URL url = new URL(linkStr);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestProperty("Accept", "text/html");

conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");

conn.setRequestProperty("Host", "www.baidu.com");

conn.setRequestProperty("Cache-Control", "max-age=0");

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36");

conn.connect();

int code = conn.getResponseCode();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));

String line = "";

htmlStr = "";

while ((line = bufferedReader.readLine()) != null)

{

htmlStr += line;

}

}

public String getHtmlStr()

{

return htmlStr;

}

}

然后创建动漫类Anime类获取动漫名字符串:

public class Anime

{

private Html html;

private ArrayList list;

public Anime(Html html)

{

this.html = html;

list = null;

}

public ArrayList getNameList()

{

String str = html.getHtmlStr();

list = SubStr.separateBetweenStr(Conver.nsb(str), "alt=\"", "\" />");

return list;

}

}

最后的Main类:

public class Main

{

public static void main(String[] args) throws Exception

{

int i = 0;

ArrayList totalName = new ArrayList<>();

for (i = 1;i <= 416;++i)

{

Html html = new Html();

html.loadHtml(i);

Anime anime = new Anime(html);

totalName.addAll(anime.getNameList());

}

Out.println(totalName);

}

}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值