zoukankan      html  css  js  c++  java
  • 2月12日学习记录

    1,背诵单词:loaf 一个面包   applicable可应用 (实施 )的   motion 运动,提议   diploma毕业文凭 ,学位证书  contrast 对比,对照   trend倾向 ,趋势  honorable可敬的    transition转变,变迁   appendix附录,附属物  objection 反对 ,异议 ,不喜欢  inflation 通货膨胀   substantial 实质的 ; 相当的   hasty匆忙的 ,仓促的  interpret 解释 ,说明   fraction 碎片 ,小部分  romance  传奇 ,爱情故事  rectangle 长方形,矩形   owl 猫头鹰  denote 表示 ,意味着   editor 编辑,编者  fruitful 多产的 concede 承认 退让   jewelry 珠宝  

    2,做爬取北京信件网页内容存入文件

      今天通过学习解决了用htmlunit获取每条信件的编号,拼接为url存入url_list文件

    通过htmlunit爬取信件编号

    package com.lq.webmagic;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.gargoylesoftware.htmlunit.*;
    import com.gargoylesoftware.htmlunit.html.HtmlDivision;
    import com.gargoylesoftware.htmlunit.html.HtmlElement;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement;
    import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
    import com.lq.file.LocalFile;
    import com.lq.file.StringHandle;
    
    public class HtmlUnit
    {
        static List<String> lines_zi=new ArrayList<String>();
        static List<String> lines_jian=new ArrayList<String>();
        static List<String> lines_tou=new ArrayList<String>();
        static String line;
        public static void Value_start()
        {
            WebClient webClient=new WebClient();
            try {
                webClient.getOptions().setActiveXNative(false);   //设置是否允许本地ActiveX组件
                webClient.getOptions().setJavaScriptEnabled(true);//启用/禁用JavaScript的支持。默认情况下,这个属性被启用。
                webClient.getOptions().setDoNotTrackEnabled(true);//启用/禁用“不跟踪”的支持。默认情况下,这个属性被禁用。
                webClient.getOptions().setThrowExceptionOnScriptError(false);//更改此Web客户端时出现脚本错误的行为。
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//指定是否也不例外将在一个失败的状态代码的情况下被抛出。成功的状态码的范围是200-299。默认值为true。
                webClient.getCache().setMaxSize(100);  //获取当前正在使用的缓存。
                webClient.setAjaxController(new NicelyResynchronizingAjaxController());//设置当前AJAX控制器 NicelyResynchronxxx():这AjaxController重新同步调用从主线程中调用。这个想法是,异步AJAX调用直接响应于用户动作(因此在“主”线程,而不是在后台任务的线程)进行直接对用户有用的。轻松拥有一个可测试的状态,这些调用同步进行。
                webClient.getOptions().setCssEnabled(false);//不支持css
                webClient.getOptions().setUseInsecureSSL(true);//如果设置为true,客户端将接受连接到任何主机,而不管他们是否有有效证件或没有。当你试图连接到使用过期或损坏的证书的服务器,这是特别有用的。
                webClient.getCookieManager().setCookiesEnabled(true); //返回此web客户端使用cookie管理器。启用/禁用cookie支持。 Cookies是默认启用
                webClient.getCache().clear();
                webClient.setRefreshHandler(new ImmediateRefreshHandler());//设置每当刷新触发所使用的处理程序 这刷新处理程序后立即刷新指定的页面,使用指定的URL,而忽略了等待时间。
                webClient.getOptions().setTimeout(2*1000);    //设置WebConnection的超时。设置为2秒
                webClient.setJavaScriptTimeout(600*1000);   //设置了一个脚本所允许被终止之前执行的毫秒数
                webClient.waitForBackgroundJavaScript(60*1000);//安排通过window.setTimeout,window.setInterval或异步的XMLHttpRequest执行JavaScript的任务。决定了后台任务等待(毫秒)延迟
                HtmlPage page = webClient.getPage("http://www.beijing.gov.cn/hudong/hdjl/com.web.search.mailList.flow");
                HtmlElement a=page.getElementByName("nextPage");
                int j=1,lastj=0;
                StringHandle sh=new StringHandle();
                List<String> lastInfo_zi=new ArrayList<String>();
                List<String> lastInfo_jian=new ArrayList<String>();
                List<String> lastInfo_tou=new ArrayList<String>();
    
                while(j!=314)
                {
                    String nowInfo=page.asXml();
                    String re="letterdetail\('.*?','AH[0-9]{11}'\)";
                    List<String> infoList_zi=sh.getExpString(re, nowInfo);
                    int g_size_zi=infoList_zi.size();
                    if(sh.StringListSameOutStringList(infoList_zi, lastInfo_zi).size()!=g_size_zi&&g_size_zi==6)
                    {
                        for(int i=0;i<g_size_zi;i++)
                        {
                            String theWeb=sh.getExpToString("AH[0-9]{11}", infoList_zi.get(i));
                            System.out.println(theWeb);
                            lines_zi.add(theWeb);
                            System.out.println("-------------------------------------------");
    
                            if(i==g_size_zi-1)
                            {
                                lastInfo_zi=infoList_zi;
                                System.out.println(j);
                                j++;
                                break;
                            }
    
                        }
                        page=a.click();
                    }
                }
    
                LocalFile localFile=new LocalFile();
                try {
                    localFile.list_local(lines_zi,"xj_list.txt");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }catch (FailingHttpStatusCodeException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                webClient.close(); //
            }
    
        }
        public static void main(String[] args) {
            Value_start();
        }
    
    }

    保存url到文件

    public void list_local(List<String> lists,String files) throws Exception// xieru xinjian de liebiao
        {
            File file=new File("outfile"+File.separator+files);
            if (!file.getParentFile().exists())
            {
                file.getParentFile().mkdirs();
            }
            file.delete();
            FileWriter local_file=new FileWriter(file);
            BufferedWriter output=new BufferedWriter(local_file);
            //Writer output=new FileWriter(local_file,true);
            for(String list:lists){
                String url="http://www.beijing.gov.cn/hudong/hdjl/com.web.consult.consultDetail.flow?originalId="+list.toString();
                String contents=url+",
    ";
                output.write(contents);
            }
            output.close();
        }

    3,遇到的问题:我想读取存入文件的每一个url用webmagic爬取每条信件的各个信息,但是爬取一条信件可以爬取,但是循环爬取多个页面不知道怎么爬取

    4,明天计划:解决用webmagic爬取多个页面,用spark进行查重操作。

  • 相关阅读:
    程序员面试金典--最大和子矩阵
    hihocoder-[Offer收割]编程练习赛25
    牛客网--今日头条2017后端工程师实习生笔试题
    牛客网-网易2017内推笔试编程题合集(二)
    hihocoder-1565-大富翁
    引用传递和值传递
    SSH详解
    Oracle中的游标
    Oracle触发器-变异表触发器不能访问本表
    Oracle中 in、exists、not in,not exists的比较
  • 原文地址:https://www.cnblogs.com/lq13035130506/p/12301748.html
Copyright © 2011-2022 走看看