zoukankan      html  css  js  c++  java
  • Java解析html页面,获取想要的元素

    背景:通过接口访问数据,获取的内容是个标准的html格式,使用jsoup的方式获取页面元素值

    先推荐比较好的博客:http://www.open-open.com/jsoup/、 单个案例比较不错

    http://blog.csdn.net/u010814849/article/details/52526582  整合内容很多

    1.插件下载并安装

    官网安装地址:http://jsoup.org/packages/jsoup-1.8.1.jar

    2.使用(目前都是用的css方式定位元素)

    1.获取这个网页的商品标题内容

    代码说明:response为页面的网页元素,一个标准的html

    Document doc = Jsoup.parse(resopnes); // 使用jsoup 进行语言转换
    String getTitle = doc.select("#goods_title").attr("value");// 商品标题 #使用css方式

    2. 获取静态页面的标题,元素input

    可直接使用浏览器的css方式:#showtab0 > tbody > tr:nth-child(2) > td:nth-child(2) > input.input_style

    Document doc = Jsoup.parse(resopnes); // 使用jsoup 进行语言转换
    String getProductName = doc.select("#showtab0 > tbody > tr:nth-child(2) > td:nth-child(2) > input.input_style").attr("value");
    System.out.println("商品名称:"+getProductName);

     

    3.获取其他说明,元素为textarea

    String detail = doc.select("#goods_desc_en").text();// 详细描述
    System.out.println("详细描述"+detail);

    
    
  • 相关阅读:
    判断闰年
    CaesarCode
    substring
    configure: error: Cannot use an external APR with the bundled APR-util
    字符串处理487-3279
    git分支管理
    git解决冲突
    git 分支的创建和切换
    nginx与php-fpm原理
    git 远程仓库与本地项目关联
  • 原文地址:https://www.cnblogs.com/chongyou/p/7478545.html
Copyright © 2011-2022 走看看