java爬虫上传文件并填充信息

javaweb 要再后台自动提交buffalo文件到网页上,采用爬虫的方式。

提交成功后:

使用的方法是httpClient,用post模拟请求。

要注意一点是文件要通过相对目录找到绝对目录,才能找到这个文件,再去上传。

        String url = "http://bdp.jd.com/buffalo/task/add-grace.html?businessType=001";
        HttpPost post = new HttpPost(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String cookie = "XXXX";
        String user_agent = "\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
        post.addHeader("Cookie",cookie);
        post.addHeader("Accept","application/json, text/javascript, */*; q=0.01");
        post.addHeader("User-Agent",user_agent);

        String filename = "mytest5.py";
        String filepath;
        filepath = request.getSession().getServletContext().getRealPath("/WEB-INF/uploads/"+filename);
        FileBody bin = new FileBody(new File(filepath));
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("jsdAppgroupId",new StringBody("10656", ContentType.TEXT_PLAIN))
                .addPart("managers",new StringBody("XXXXX", ContentType.TEXT_PLAIN))
                .addPart("description",new StringBody("测试", ContentType.TEXT_PLAIN))
                .addPart("calEngine",new StringBody("hive", ContentType.TEXT_PLAIN))
                .addPart("verDescription",new StringBody("测试", ContentType.TEXT_PLAIN))
                .addPart("model",new StringBody("001", ContentType.TEXT_PLAIN))
                .addPart("param",new StringBody("[{\"name\":\"hive\",\"desc\":\"hive\"}]", ContentType.TEXT_PLAIN))
                .build();

        post.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(post);

        //结果显示
        InputStream in = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
        StringBuilder strber= new StringBuilder();
        String line = null;
        while((line = br.readLine())!=null){
            strber.append(line+'\n');
        }
        br.close();
        in.close();
        String result = strber.toString();
        logger.info(result);

一种webClient的方法,可以简单点击和填充:

        WebClient webClient = new WebClient();
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);

        //登录
        HtmlPage page =  webClient.getPage("http://bdp.jd.com/login.html");
        HtmlForm htmlForm = page.getForms().get(0);
        HtmlInput erp = htmlForm.getInputByName("erp");
        HtmlInput password = htmlForm.getInputByName("password");
        erp.setValueAttribute("xxx");
        password.setValueAttribute("xxx");
        HtmlPage page2 = htmlForm.getElementsByTagName("button").get(0).click();
        logger.info(page2.asXml());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值