任务:爬取某地方法院的裁判文书,并将内容抽取出来保存在excel中

爬虫小白,用最简单粗暴的方法爬虫,研究要爬虫的网页源代码结构,用正则表达式抽取出自己想要的内容

我爬取的地方法院的裁判文书网址链接样式如下

地方法院的网址:s_url = "http://xxxxx.xxxxxxxxxx.xxx"

列举裁判文书具体链接的url:m_url = s_url + /paper/more/……

每一篇裁判文书的url:d_url = s_url + /paper/detail/……

抽取m_url的方法

private static void matchMoreLink(ArrayList search_list,String input){//The function that match links that contains paper links and add it to the search list//arg0:arraylist that contains the links//arg1:web page contentPattern pattern =Pattern.compile("href=\"/paper/more/.*?\"");Matcher matcher=pattern.matcher(input);while(matcher.find()){String l=matcher.group().replace("href=","");l=l.replaceAll("\"","");//System.out.println(l+"\n");l=s_url+l;if(!search_list.contains(l)) {//If the link don't in the search list, add it to the listsearch_list.add(l);}}
}

抽取d_url的方法

private static void matchDetailLink(HashSet set,String input,StringBuffer result) {//The function that match paper links and add to the set and StringBuffer//arg0:set that contains paper links//arg1:web page content//arg2:StringBuffer for storing the string of linksPattern pattern =Pattern.compile("href=\'/paper/detail/.*?\'");Matcher matcher=pattern.matcher(input);while(matcher.find()){String l=matcher.group().replace("href=","");l=l.replaceAll("'","");l=s_url+l;if(!set.contains(l)) {result.append(l+"\n");set.add(l);}}
}

使用数组列表存储所有爬下来的m_url,使用集合存储所有爬下来的d_url

由于在这些网页中不仅有许多裁判文书的具体链接,还有一些通向另外的m_url的链接,存在数组列表中,然后按列表顺序t通过m_url爬取下更多的m_url和d_url,判断爬下来的m_url是否已经在list中有,没有就追加在末尾,判断爬下来的d_url是否已经在set中有,没有就加入

(这样做很粗糙,列表和数组的大小有限,没有很多链接的情况下没有什么问题,如果链接太多就不行了,大概查询了解决方法,可以使用数据库、限制爬虫层数等,反正只是先爬下裁判文书的链接,也可以限制一下爬虫层数,将d_url保存在文件中,然后去重之后再根据d_url去爬取裁判文书,去重方法就很多啦,链接去重也比内容去重简单多了)

将列表中的m_url都爬取完之后,也就获得了网站所有的d_url,也就是所有裁判文书的链接

(由于不一定能一次爬取完毕,为了能够多次爬取,可以将爬取过的d_url保存在文件中)

获取了d_url之后,就是通过d_url获取裁判文书内容,但是可能是访问太快或者频繁访问时网站就会使用重定向,不是直接给你返回网页内容,而是给了另一个链接

据说这是反爬虫的一种,可以加好几层,但是还好这个网站只要再顺着给出的链接就可以到达真正的内容页

private static String getHrefContent(String input) throws Exception {//The function that match the real content and return the content//arg:The content from the web page that use dynamic output jumpPattern pattern =Pattern.compile("window.location.href='.*?'");Matcher matcher=pattern.matcher(input);String url=null;if(matcher.find()) {url=matcher.group().replace("window.location.href=", "");url=url.replaceAll("'", "");}return getPage(url);
}

内容页的反爬可能是最难的地方

根据d_url获取的网页源代码的用于显示裁判文书内容的部分是一段javascript代码

百度了一下,得使用javascript获取内容

参考了这篇文章https://blog.csdn.net/u012979457/article/details/80053033

点击圈内的链接,可以获取以下代码

对于这种eval开头的,说明还要js解密获取真正的javascript方法

http://www.cnblogs.com/dudumao/archive/2012/10/05/2712156.html

在上面的博客中GET到了解密工具,懒,直接复制下代码用解密工具解

可能会不止加一重,一直点击解码,直到代码中开头没有eval

很明显paperDecode就是解密的javascript函数了

再观察一下用于显示内容的javascript函数

每一篇裁判文书其实只有tm[0],tm[1]的值不同,那么我们可以爬虫时把这两个值爬下来再另外解出内容

于是写一段javascript代码保存为getPaper.js,可以接受传入的tm[0],tm[1]的值,返回解出的内容

function paperDecode(paperString){var ret='';paperString=unescape(paperString);for(var i=paperString.length;i>0;i--){ret+=paperString.substr(i-1,1)}return ret
}var tm=new Array(2) html='';
show=function(a,b){tm[0]=a;tm[1]=b;html+=paperDecode(tm[0]);html+=paperDecode(tm[1]);return html
}

在java中,使用javax.script包调用getPaper.js获取内容

public static String getPaper(String input) throws Exception {//The function that get paper from the content(source code of the web page)//arg:the content(source code of the web page)//The web page use javascript to encrypt the paperString a = null,b=null,result=null;Pattern pattern =Pattern.compile("tm\\[0\\]=\".*?\"");Matcher matcher=pattern.matcher(input);if(matcher.find()) {a=matcher.group().replace("tm[0]=", "");            }pattern =Pattern.compile("tm\\[1\\]=\".*?\"");matcher=pattern.matcher(input);if(matcher.find()) {b=matcher.group().replace("tm[1]=", "");         }//match the encrypted content codeScriptEngineManager manager = new ScriptEngineManager();   ScriptEngine engine = manager.getEngineByName("javascript"); String jsFileName = "getPaper.js";FileReader reader = new FileReader(jsFileName);engine.eval(reader);   if(engine instanceof Invocable) {    Invocable invoke = (Invocable)engine;result = (String)invoke.invokeFunction("show", a, b);}   reader.close();   //use a script file(getPaper.js) to get the decrypted contentreturn result;
}

这里踩了一个坑,使用正则匹配时表达式中的中括号前要加两个转移符(“\\")

这样子爬去下来的内容是带html标签的,可以在抽取内容的时候顺便去掉标签

在爬取文书之前先使用poi包生成excel文件(或者之间新建一个excel文件,但要注意excel类型和后续写入使用的类是对应的)

private static File getExcelFile(String filename)  {//The function that create a excel file (filename.xls)//arg:file name of the target excel fileFile file=new File(filename);FileOutputStream fout=null;HSSFWorkbook workbook=new HSSFWorkbook();HSSFSheet sheet=workbook.createSheet("Paper");sheet.setColumnWidth(0, 3*256);sheet.setColumnWidth(1, 30*256);sheet.setColumnWidth(2, 27*256);sheet.setColumnWidth(3, 20*256);sheet.setColumnWidth(4, 12*256);sheet.setColumnWidth(5, 25*256);sheet.setColumnWidth(6, 80*256);HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true);//设置自动换行 HSSFRow row=sheet.createRow(0);HSSFCell cell;for(int i=0;i<7;i++) {cell=row.createCell(i);cell.setCellType(CellType.STRING);cell.setCellStyle(cellStyle);}row.getCell(0).setCellValue("No");row.getCell(1).setCellValue("Title");row.getCell(2).setCellValue("Time");row.getCell(3).setCellValue("Place");row.getCell(4).setCellValue("Type");row.getCell(5).setCellValue("Tag");row.getCell(6).setCellValue("content");try {fout=new FileOutputStream(file);workbook.write(fout);fout.flush();fout.close();System.out.println("Successfully create a excel file !");} catch (IOException e) {System.out.println("Fail to create a excel file");return null;}finally {if(fout!=null)try {fout.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return file;}

由于正文结构很简单,直接用标签把内容抽取出来就行

 public static void writeExcel(String paper,File file) {//The function that write papers into the excel file//arg0:paper that would like to write to excel//arg1:target excel fileString title=null,time=null,place=null,type=null,tag=null,content=null;String html_regex = "<[^>]+>";//regular expressions for HTML tagsPattern pattern =Pattern.compile("<li class=\"title\">.*?</li>");Matcher matcher=pattern.matcher(paper);if(matcher.find()) {title=matcher.group();title=title.replaceAll(html_regex, "");}        //match the title of paperpattern=Pattern.compile("<li class=\"time\">.*?</li>");matcher=pattern.matcher(paper);if(matcher.find()) {time=matcher.group();time=time.replaceAll(html_regex, "");}        //match the publish time of paperpattern=Pattern.compile("<b>.*?</b>");matcher=pattern.matcher(paper);if(matcher.find()) {place=matcher.group();place=place.replaceAll(html_regex, "");}//match the publish place of paperpattern=Pattern.compile("<li class=\"type\">.*?</li>");matcher=pattern.matcher(paper);if(matcher.find()) {type=matcher.group();type=type.replaceAll(html_regex, "");type=type.replace(" ", "");}//match the type of paperpattern=Pattern.compile("<li class=\"number\">.*?</li>");matcher=pattern.matcher(paper);if(matcher.find()) {tag=matcher.group();tag=tag.replaceAll(html_regex, "");}//match the tag of paperpattern=Pattern.compile("<p>.*?</p>");matcher=pattern.matcher(paper);StringBuffer sb=new StringBuffer();String l;while(matcher.find()) {l=matcher.group().replaceAll("&nbsp;", " ");if(l==null)continue;sb.append(l.replaceAll(html_regex, "")+"#");}content=sb.toString();//match the content of the paperFileInputStream fin=null;FileOutputStream fout=null;try {fin=new FileInputStream(file);HSSFWorkbook workbook=new HSSFWorkbook(fin);fin.close();HSSFSheet sheet=workbook.getSheetAt(0);int ri=sheet.getLastRowNum()+1;HSSFRow row=sheet.createRow(ri);HSSFCell cell=null;for(int i=0;i<7;i++) {cell=row.createCell(i);cell.setCellType(CellType.STRING);}String no=String.valueOf(ri);row.getCell(0).setCellValue(no);row.getCell(1).setCellValue(title);row.getCell(2).setCellValue(time);row.getCell(3).setCellValue(place);row.getCell(4).setCellValue(type);row.getCell(5).setCellValue(tag);row.getCell(6).setCellValue(content);fout=new FileOutputStream(file);workbook.write(fout);fout.flush();fout.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(fin!=null) {try {fin.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fout!=null) {try {fout.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//write the document to a new row of the excel file}

记录向 | 爬虫 | 裁判文书爬取(java)相关推荐

  1. layui获取input信息_python爬虫—用selenium爬取京东商品信息

    python爬虫--用selenium爬取京东商品信息 1.先附上效果图(我偷懒只爬了4页) 2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Hea ...

  2. Python爬虫---影评的爬取

    Python爬虫-影评的爬取 介绍:爬虫练手,使用Requests库进行豆瓣影评的爬虫,做成词云图,写文章主要做一些问题解决的记录. 运行环境:python 3.8, Pycharm 关于在豆瓣爬取影 ...

  3. 【Python爬虫】从零开始爬取Sci-Hub上的论文(串行爬取)

    [Python爬虫]从零开始爬取Sci-Hub上的论文(串行爬取) 维护日志 项目简介 步骤与实践 STEP1 获取目标内容的列表 STEP2 利用开发者工具进行网页调研 2.1 提取文章链接和分页链 ...

  4. [Python 爬虫] 使用 Scrapy 爬取新浪微博用户信息(四) —— 应对反爬技术(选取 User-Agent、添加 IP代理池以及Cookies池 )

    上一篇:[Python 爬虫] 使用 Scrapy 爬取新浪微博用户信息(三) -- 数据的持久化--使用MongoDB存储爬取的数据 最近项目有些忙,很多需求紧急上线,所以一直没能完善< 使用 ...

  5. [Python 爬虫] 使用 Scrapy 爬取新浪微博用户信息(二) —— 编写一个基本的 Spider 爬取微博用户信息

    上一篇:[Python 爬虫] 使用 Scrapy 爬取新浪微博用户信息(一) -- 新建爬虫项目 在上一篇我们新建了一个 sina_scrapy 的项目,这一节我们开始正式编写爬虫的代码. 选择目标 ...

  6. [Python 爬虫] 使用 Scrapy 爬取新浪微博用户信息(三) —— 数据的持久化——使用MongoDB存储爬取的数据

    上一篇:[Python 爬虫] 使用 Scrapy 爬取新浪微博用户信息(二) -- 编写一个基本的 Spider 爬取微博用户信息 在上一篇博客中,我们已经新建了一个爬虫应用,并简单实现了爬取一位微 ...

  7. Python爬虫实战之爬取糗事百科段子

    Python爬虫实战之爬取糗事百科段子 完整代码地址:Python爬虫实战之爬取糗事百科段子 程序代码详解: Spider1-qiushibaike.py:爬取糗事百科的8小时最新页的段子.包含的信息 ...

  8. python3 [爬虫入门实战]爬取熊猫直播用户信息

    爬取国内各大直播平台直播信息是以后要做的一个功课,还必须是做成一个系列的,可能远没有其他大神那么厉害,毕竟自己经历过的就是有用的,在此做个记录一下 首先我们需要爬取的内容: 这里我们要爬取的有 直播房 ...

  9. monthy python爬虫_Python爬虫DOTA排行榜爬取实例(分享)

    Python爬虫DOTA排行榜爬取实例(分享) 1.分析网站 打开开发者工具,我们观察到排行榜的数据并没有在doc里 doc文档 在Javascript里我么可以看到下面代码: ajax的post方法 ...

  10. 爬虫系列-jsoup爬取网页你需要了解的一切

    爬虫系列-jsoup爬取网页 概述 解析和遍历文档 文档的对象模型 加载HTML数据 从String解析文档 从String中加载解析片段 从URL加载文档 描述 从文件加载文档 描述 提取数据 使用 ...

最新文章

  1. Lock、ReentrantLock、ReentrantReadWriteLock原理及应用深入解析
  2. CSS3圆圈动画放大缩小循环动画效果
  3. 什么?你竟然还没有用这几个chrome插件?
  4. Object之MemberwiseClone方法
  5. java操作数据库 jdbc
  6. 数据平面开发套件(DPDK)中的Vhost / Virtio的配置和性能
  7. Python集合set与frozenset的区别
  8. 自然语言处理领域重要论文资源全索引
  9. 有道翻译爬虫 js逆向
  10. oracle把修改成及联,oracle日常操作命令手册(用户管理)-从零到无
  11. 人脸识别图片base64编码,Java实现
  12. Ebean报错java.lang.ClassCastException: com.project.model.xxx cannot be cast to com.project.model.xxx
  13. AMD RX 7700XT、7800XT和7600显卡参数 RX 7700XT、7800XT和7600相当于什么水平
  14. 非线性控制2.0——模糊逼近
  15. C++:给定一个二维点集,找到所有的整体对称轴
  16. hive Sql列转行使用explode的注意事项-null值处理
  17. 视频编解码之数字视频介绍
  18. 计算机考证哪些最有用
  19. 数据库连接JDBC原理
  20. python项目成功打包成exe,运行exe时报错:Unhandled exception in script:Failed to excute

热门文章

  1. jQuery-放大镜
  2. php海外话费充值游戏币,海外(国外)充值话费方法
  3. 圣诞表白html,圣诞节表白,最浪漫的表白方式
  4. ecshop mysql 标题表_ECSHOP商城全站自定义TITLE标题设置
  5. css中只读,是否可以通过CSS将输入字段设置为只读?
  6. 51单片机:点亮LED灯
  7. 嵌入式技术与应用专业毕业以后可以做什么?
  8. 香港渣打银行开户价格是多少?
  9. 追捕文件WRY.DLL的浅显分析及程序示例
  10. python后端 工作 知乎_[Python]知乎后端实习生面试心得