首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

爬虫其实很简单!——网络爬虫快速实现(一)

今天我来带领大家一起学习编写一个网络爬虫!其实爬虫很简单,没有想象中那么难,也许所有学问都是这样,恐惧源自于无知。废话不多说,现在开始我们的爬虫之旅吧。 爬虫是什么? 我们时常听说编程大牛嘴边一直念叨着“网络爬虫“,那网络爬虫究竟是何方神圣呢? 网络爬虫能够模仿用户浏览网页,并将所想要的页面中的信息保存下来。有些同学不禁要问:“我自己浏览网页,可以手动将数据保存下来啊,为何要写个程序去爬取数据呢?“道理其实很简单,程序能够在短时间内访问成千上万的页面,并且在短时间内将海量数据保存下来,这速度可远远超越了

07

Jsoup介绍及解析常用方法

jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操作方法来取出和操作数据 jsoup的主要功能如下: 从一个URL,文件或字符串中解析HTML; 使用DOM或CSS选择器来查找、取出数据; 可操作HTML元素、属性、文本; jsoup解析 Jsoup提供一系列的静态解析方法生成Document对象 static Document parse(File in, String charsetName) static Document parse(File in, String charsetName, String baseUri) static Document parse(InputStream in, String charsetName, String baseUri) static Document parse(String html) static Document parse(String html, String baseUri) static Document parse(URL url, int timeoutMillis) static Document parseBodyFragment(String bodyHtml) static Document parseBodyFragment(String bodyHtml, String baseUri) 其中baseUri表示检索到的相对URL是相对于baseUriURL的 其中charsetName表示字符集 Connection connect(String url) 根据给定的url(必须是http或https)来创建连接 Connection 提供一些方法来抓去网页内容 Connection cookie(String name, String value) 发送请求时放置cookie Connection data(Map<String,String> data) 传递请求参数 Connection data(String... keyvals) 传递请求参数 Document get() 以get方式发送请求并对返回结果进行解析 Document post()以post方式发送请求并对返回结果进行解析 Connection userAgent(String userAgent) Connection header(String name, String value) 添加请求头 Connection referrer(String referrer) 设置请求来源 jsoup提供类似JS获取html元素: getElementById(String id) 用id获得元素 getElementsByTag(String tag) 用标签获得元素 getElementsByClass(String className) 用class获得元素 getElementsByAttribute(String key) 用属性获得元素 同时还提供下面的方法提供获取兄弟节点:siblingElements(), firstElementSibling(), lastElementSibling();nextElementSibling(), previousElementSibling() 获得与设置元素的数据 attr(String key) 获得元素的数据 attr(String key, String value) 设置元素数据 attributes() 获得所以属性 id(), className() classNames() 获得id class得值 text()获得文本值 text(String value) 设置文本值 html() 获取html html(String value)设置html outerHtml() 获得内部html data()获得数据内容 tag() 获得tag 和 tagName() 获得tagname 操作html元素: append(String html), prepend(String html) appendText(String text), prependText(String text) appendElement(String tagName), prependElement(String tagName) html(String value) jsoup还提供了类似于JQuery方式的选择器 采用选择器来检索

02
领券