java利用Jsoup做爬虫简单方法示例

1.限制太多,与反扒斗智斗勇.需要分析,项目不一定能爬到,只是方法示例

项目地址: 

链接:https://pan.baidu.com/s/1jkhT4mJqP_tsDaN2VEJiZw 
提取码:nsyu 
复制这段内容后打开百度网盘手机App,操作更方便哦

1.pom.xml文件

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
         <!--fastJson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

JS请求获取数据类型

1.分析网页,观察请求,分析规律

2.js请求获取数据类型的爬取,就能拿到数据,解析数据,分析数据即可

String parentUrl = "https://movie.douban.com/j/search_tags?type=tv&source=";
String title = Jsoup.connect(parentUrl)
                .ignoreContentType(true).get().body().text();
JSONArray tags = JSONArray.parseObject(title).getJSONArray("tags");

数据嵌套在页面中

1.分析每一个图书都是一个li标签的class=subject-item包裹着,所以获取这个图书信息的代码为

 String url = "https://book.douban.com/tag/小说";
 Element e = Jsoup.connect(url).get().select(".subject-item").get(0);

这样就获取到了所有的class=subject-item 下的图书了

2.获取标题 可以看出标题在subject-item  class下的 class=info的div下. div下的h2标签 的a标签下 ,所以代码一步步选中

Element a2 = e.select(".info").get(0);  //获取info下的数据
String title = a2.select("h2").select("a").attr("title"); //图书名称

3.就获取到了title值为坏小孩

4.其他数据同理获取

模拟浏览器登录

1.登录,查看cookic信息

      Map<String, String> cookies = new HashMap();
        cookies.put("loc-last-index-location-id", "118254");
        cookies.put("dbcl2", "164885001:pu3jCf6Fsls");

2.模拟登录发起请求

Elements els = Jsoup.connect(url)
         .header("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
         .cookies(cookies)
         .get().select(".tagCol").select("a");

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值