美文网首页UFEG-A
java爬虫系列(一)——利用httpclient获取磁力链接

java爬虫系列(一)——利用httpclient获取磁力链接

作者: 如果在这里看见他请叫他去学习 | 来源:发表于2018-09-30 10:56 被阅读565次

      最近呢再看有关爬虫方面相关的文章,然后想了想,写一些平时有可能常用的小工具。想必大家平时也会在网上找一些资源大多会用到磁力搜索,(这里以https://www.102436.com磁力搜索网站为例);用到的工具包为HttpClient4.5版本 和jsoup1.11版本包,开发语言为java。也用到了一个maven的仓库管理,毕竟下载jar包很方便,具体的实现代码为:

    首先是添加maven的依赖,获取httpclient与jsoup 相关jar包  

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->

    <dependency>

        <groupId>org.apache.httpcomponents</groupId>

        <artifactId>httpclient</artifactId>

        <version>4.5.3</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->

    <dependency>

        <groupId>org.jsoup</groupId>

        <artifactId>jsoup</artifactId>

        <version>1.11.3</version>

    </dependency>

    具体代码

    /* 输入关键词进行磁力搜索

    * 举例网站(https://www.102436.com/cili)

    * keyword:关键词

    */

    public static void getMagnet(String keyword){

    try {

    //获取磁力链网站

    Document document = Jsoup.connect("https://www.102436.com/cili/"+keyword+".html").userAgent("Mozilla").get();

    //获取搜索出来的链接

    Elements elements = document.select(".list-group a");

    //遍历

    for (Element element2 : elements) {

    //获取a标签中的href值

    String href = element2.attr("href");

    //进入超链接

    Document hrefdoc = Jsoup.connect("https://www.102436.com"+href).userAgent("Mozilla").get();

    Elements me =hrefdoc.select(".panel-body p a");

    for (Element element : me) {

    //打印所有的磁力链接

    System.out.println(element2.text()+":"+element.attr("href"));

    }

    }

    } catch (Exception e) {

    // TODO: handle exception

    }

    }

    这其实就是利用jsoup包中的方法解析html,根据具体需求获取标签里的属性,跟js操作dom的方法类似(document.querySelector),获取对象,然后遍历。。。

    后期也想继续研究下解析磁力链接下载文件,或是做个页面。。。

    这里在附上一张搜索图

    相关文章

      网友评论

        本文标题:java爬虫系列(一)——利用httpclient获取磁力链接

        本文链接:https://www.haomeiwen.com/subject/wgeboftx.html