首页 新闻 赞助 找找看

java模拟浏览器登录,进去爬取数据

0
悬赏园豆:5 [已解决问题] 解决于 2018-06-19 17:19

最近都在弄爬虫,网上的资料中java爬虫的资料并不多,主要的还是python的爬虫资料。我弄了好久也没把那个网站登录进去,主要是该网站登录跳转,而且请求很多,。有哪位老铁能指导一下吗?

同城旧气的主页 同城旧气 | 初学一级 | 园豆:198
提问于:2018-06-05 18:37
< >
分享
最佳答案
0

java可以通过jsoup爬取网页内容

另外还推荐一个java爬虫框架WebCollector

收获园豆:5
ycyzharry | 高人七级 |园豆:25639 | 2018-06-06 09:59

你有相关的demo吗,或者以前工作中用到的案例能分享一下吗,老铁

同城旧气 | 园豆:198 (初学一级) | 2018-06-06 10:49

@同城旧气: 手写一个Java爬虫

ycyzharry | 园豆:25639 (高人七级) | 2018-06-06 11:17

@ycyzharry: 我现在遇到个问题,就是jsoup的post请求需要带非String类型的参数(bool型,date型的),,在网上没找到相关的代码,我的代码如下,不知道是不是参数类型不正确导致没请求没返回正确的数据?有没有办法可以解决支持传不同类型的参数呢。

同城旧气 | 园豆:198 (初学一级) | 2018-06-07 11:28
其他回答(2)
0

phantomjs了解一下

jstarseven | 园豆:204 (菜鸟二级) | 2018-06-06 14:32

好的,谢谢!

支持(0) 反对(0) 同城旧气 | 园豆:198 (初学一级) | 2018-06-07 11:20
0
package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

import com.alibaba.fastjson.JSONObject;

public class postt {
    public static void main(String[] args) {
        JSONObject params = new JSONObject();
        params.put("参数名", "参数值");
        params.put("参数名", "参数值");
        String sr = postt.sendPost("请求网址",params);
        System.out.println(sr);
    }

    public static String sendPost(String url, JSONObject param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type","application/json");
            conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
}
DanBrown | 园豆:1321 (小虾三级) | 2018-06-08 09:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册