java简单爬虫小demo爬信息为主

/**
 * 基础实现对网页字段的抓取
 * Created by 耿直 on 2017/11/29.
 */
public class Html {
    public Document getHtmlTextByUrl(String url) {

        Document doc = null;
        int i = (int) (Math.random() * 1000);
        while (i != 0) {
            i--;
        }
        try {
            doc = Jsoup.connect(url).data("query", "Java").userAgent("Mozilla").cookie("auth", "token").timeout(300000).post();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            doc = Jsoup.connect(url).timeout(500000).post();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }


    /**
     * 获取树节点<tr><td></td></tr>
     *
     * @param doc
     * @param className
     * @return
     */
    public Elements getElementByClass(Document doc, String className) {
        Elements elements = null;
        elements = doc.select(className);
        return elements;
    }

    /**
     * 调用
     *
     * @param name
     * @param url  网址
     * @param type provincertr
     * @return
     */
    public ArrayList getProvince(String name, String url, String type) {
        ArrayList result = new ArrayList();
        String classType = "." + type;
        Document doc = this.getHtmlTextByUrl(url);
        if (doc != null) {
            Elements elements = this.getElementByClass(doc, classType);
            for (Element item : elements) {
                if (item != null) {
                    for (Element items : item.children()) {
                        String[] str = new String[4];
                        if (items.children().first() != null) {
                           // str[0] = url;
                            str[1] = items.children().first().ownText();
                            String ownUrl = items.children().first().attr("href");
                            str[2] = ownUrl;
                            str[3] = type;
                            result.add(str);
                        }
                    }
                }
            }
        }
        return result;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值