`
liuzejian4
  • 浏览: 58190 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Use DOM methods to navigate a document(使用DOM方法来操纵文档)

 
阅读更多
Problem
You have a HTML document that you want to extract data from(你想从一个html文件中提取数据). You know generally the structure of the HTML document(你知道html的结构).

Solution
Use the DOM-like methods available after parsing HTML into a Document.
(使用类似dom操作的方法来解析html文档)
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
  String linkHref = link.attr("href");
  String linkText = link.text();
}

Description
Elements provide a range of DOM-like methods to find elements(提供一系列类似于dom方法来找到元素), and extract and manipulate their data(提取和操纵数据). The DOM getters are contextual(): called on a parent Document they find matching elements under the document; called on a child element they find elements under that child. In this way you can winnow in on the data you want.

Finding elements(查找元素方法)
getElementById(String id)
getElementsByTag(String tag)
getElementsByClass(String className)
getElementsByAttribute(String key) (and related methods)
Element siblings: siblingElements(), firstElementSibling(), lastElementSibling(); nextElementSibling(), previousElementSibling()
Graph: parent(), children(), child(int index)
Element data(元素数据)
attr(String key) to get and attr(String key, String value) to set attributes
attributes() to get all attributes
id(), className() and classNames()
text() to get and text(String value) to set the text content
html() to get and html(String value) to set the inner HTML content
outerHtml() to get the outer HTML value
data() to get data content (e.g. of script and style tags)
tag() and tagName()
Manipulating HTML and text(操作html 和 文本)
append(String html), prepend(String html)
appendText(String text), prependText(String text)
appendElement(String tagName), prependElement(String tagName)
html(String value)
分享到:
评论

相关推荐

    c#操作浏览器dom对象

    例如,你可以使用`InvokeScript`方法来执行JavaScript代码,从而与DOM进行更复杂的交互: ```csharp string jsCode = "document.getElementById('myElement').style.backgroundColor = 'red';"; webBrowser1....

    Dom编程基础.pdf

    例如,你可以使用`document.getElementById()`方法获取特定ID的元素,然后修改它的属性,如`element.style.display`来控制元素的可见性。 事件是DOM编程中的关键部分,它们允许网页响应用户的操作。例如,`...

    java+selenium 使用Get、Navigate系列方法操作网页

    //使用get方法来打开慕课网首页 driver.get("https://www.imooc.com"); // 使用navigate().refresh()方法刷新页面 driver.navigate().refresh(); //使用navigate.to方法打开第二个页面:免费课 driver.navigate()....

    Learning to Navigate for Fine-grained Classification

    ECCV 2018 paper, Fine-grained image recognition,propose a novel self-supervision mechanism to effectively localize informative regions without the need of bounding-box/part annotations

    angular2中router路由跳转navigate的使用与刷新页面问题详解

    本文将详细介绍Angular2中Router路由跳转Navigate的使用方法和解决刷新页面问题的方法。 一、Router.navigate的使用 Router.navigate是Router类的一个方法,主要用来跳转路由。其函数定义为:navigate(commands: ...

    微信小程序wx.navigateTo方法里的events参数使用详情及场景

    在微信小程序开发中,`wx.navigateTo`方法是一个关键的页面跳转函数,它允许开发者从一个页面导航到另一个页面,而无需关闭当前页面。在2.7.3版本中,`wx.navigateTo`增加了一个名为`events`的参数,这个新特性极大...

    Dom编程基础[参考].pdf

    在JavaScript中,DOM提供了丰富的API,允许开发者通过调用对象的属性和方法来控制页面元素。例如,要获取或设置文本框的值,你可以直接操作对应的DOM对象,如`document.getElementById('myInput').value`。DOM还支持...

    微信小程序出现wx.navigateTo页面不跳转问题的解决方法

    本文实例讲述了微信小程序出现wx.navigateTo页面不跳转问题的解决方法。分享给大家供大家参考,具体如下: 今天在做微信小程序的时候,做的商品购买流程。流程是这样的: 商品展示–>商品详情–>下单页面–>下单详情...

    VB中WEBBROWSER技巧大全(DOC文档)

    使用`Navigate`方法时,可以通过传入特定参数来控制导航行为。 #### 十六、本地文件收藏夹操作 可以通过操作`Favorites`对象来添加、删除或编辑本地收藏夹中的项目。 #### 十七、让Webbrowser全屏 通过设置...

    微信小程序wx.navigateTo中events属性实现页面间通信传值,数据同步

    小程序wx.navigateTo路由方法中新增加了events属性,可以用于监听被打开页面发送到当前页面的数据。基础库 2.7.3 开始支持。 相关文档链接:...

    DOM视频笔记

    22. **日期与时间**: 使用`Date`对象及其方法来获取当前时间或日期的各部分信息。 23. **DOM元素创建**: 通过`createElement()`方法创建新的DOM元素,并设置其属性和类型。 以上是基于给定文件内容整理出的DOM相关...

    微信小程序插件 navigateTo:fail rejected due to no permission currently

    在微信小程序开发中,"navigateTo:fail rejected due to no permission currently" 是一个常见的错误提示,通常发生在尝试使用`navigateTo`方法跳转页面时,由于权限问题导致操作失败。这个错误是由于开发者未正确...

    Open Source Intelligence Methods and Tools

    Open Source Intelligence Methods and Tools focuses on building a deep understanding of how to exploit open source intelligence (OSINT) techniques, methods, and tools to acquire information from ...

    微信小程序使用navigateTo数据传递的实例

    微信小程序使用navigateTo数据传递的实例是指在微信小程序中使用navigateTo API来传递数据的实例。该实例主要介绍了如何使用navigateTo API来传递基本数据类型、对象和数组集合等数据类型。 一、传递基本数据类型 ...

    杨中科 传智播客 DOM 学习笔记

    根据给定的文件信息,我们可以总结出一系列与DOM(Document Object Model)操作、JavaScript事件处理以及浏览器窗口操作相关的IT知识点。以下是对这些知识点的详细解释: ### 1. DOM Event Handling(DOM事件处理)...

Global site tag (gtag.js) - Google Analytics