【企业岗位需求决策--数据采集】

企业岗位需求决策–数据采集
实验答案:

package net.educoder;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class App {
    public static void main(String[] args) throws IOException {
        String content = "";
        WebClient webClient = new WebClient();
        webClient.getOptions().setCssEnabled(false); // 取消 CSS 支持
        webClient.getOptions().setJavaScriptEnabled(false); // 取消 JavaScript支持
        HtmlPage page = webClient.getPage("file:/data/workspace/myshixun/step1/1.html");
        /**
         * 补充xpath规则
         *      比如:page.getByXPath("//tr")
         */

        /*-------------------begin--------------------*/
        List<HtmlElement> byXPath = page.getByXPath("//tr[@class='even'] | //tr[@class='odd']");
        /*-------------------end--------------------*/

        /**
         * 1.遍历byXPath集合
         * 2.获取HtmlElement的文本内容
         *  方法提示  -->  HtmlElement().asText()
         *  为了确保数据的规范,请使用 String().trim() 方法对字符串进行排空(排去空字符串)
         * 3.将获取的文本内容拼接到content字符串内,并使用 "\n"进行字符串隔开
         */
        /*-------------------begin--------------------*/
       for (HtmlElement he:byXPath) {
            
            String text = he.asText().trim()+"\n";
            content += text;
        }


        /*-------------------end--------------------*/
        File file = new File(args[1]);
        OutputStreamWriter oStreamWriter = new OutputStreamWriter(new        FileOutputStream(file), "utf-8");
        oStreamWriter.append(content);
        oStreamWriter.close();
    }
}

实验结果:
实验结果

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值