jsoup爬虫爬取网页
2015-11-21 19:07
661 查看
1.
你需要从一个网站获取和解析一个HTML文档,并查找其中的相关数据。你可以使用下面解决方法:
使用
和
IOException,应适当处理。
这个方法只支持Web URLs (
假如你需要从一个文件加载,可以使用
2
获取元素
Document doc
= Jsoup.connect(url).get();
3.
遍历元素
你有一个HTML文档要从中提取数据,并了解这个HTML文档的结构。
将HTML解析成一个
Elements这个对象提供了一系列类似于DOM的方法来查找元素,抽取并处理其中的数据。具体如下:
Element siblings:
Graph:
4.
需要使用的是jsoup-1.7.3.jar包 如果需要看文档我下载请借一步到官网:http://jsoup.org/
这里贴一下我用到的 Java工程的测试代码
下面来介绍android中使用Jsoup异步解析网页的数据 请注意: 这里很容易遇到一个乱码的问题
配置文件:AndroidManifest.xml中加 权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
layout的布局文件
主要异步加载数据的代码
从一个URL加载一个Document
存在问题
你需要从一个网站获取和解析一个HTML文档,并查找其中的相关数据。你可以使用下面解决方法:
解决方法
使用 Jsoup.connect(String url)方法:
Document doc = Jsoup.connect("http://example.com/").get(); String title = doc.title();
说明
connect(String url)方法创建一个新的
Connection,
和
get()取得和解析一个HTML文件。如果从该URL获取HTML时发生错误,便会抛出
IOException,应适当处理。
Connection接口还提供一个方法链来解决特殊请求,具体如下:
Document doc = Jsoup.connect("http://example.com") .data("query", "Java") .userAgent("Mozilla") .cookie("auth", "token") .timeout(3000) .post();
这个方法只支持Web URLs (
http和
https协议);
假如你需要从一个文件加载,可以使用
parse(File in, String charsetName)代替。
2
获取元素
Document doc
= Jsoup.connect(url).get();
doc.getElementsByTag("body");
Document.body()方法能够取得文档body元素的所有子元素
Elements ListDiv = doc.getElementsByAttributeValue("class","PostContent");
3.
遍历元素
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(); }
使用DOM方法来遍历一个文档
问题
你有一个HTML文档要从中提取数据,并了解这个HTML文档的结构。
方法
将HTML解析成一个Document之后,就可以使用类似于DOM的方法进行操作。示例代码:
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(); }
说明
Elements这个对象提供了一系列类似于DOM的方法来查找元素,抽取并处理其中的数据。具体如下:
查找元素
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)
元素数据
attr(String key)获取属性
attr(String key, String value)设置属性
attributes()获取所有属性
id(),
className()and
classNames()
text()获取文本内容
text(String value)设置文本内容
html()获取元素内HTML
html(String value)设置元素内的HTML内容
outerHtml()获取元素外HTML内容
data()获取数据内容(例如:script和style标签)
tag()and
tagName()
操作HTML和文本
append(String html),
prepend(String html)
appendText(String text),
prependText(String text)
appendElement(String tagName),
prependElement(String tagName)
html(String value)
4.
使用Jsoup 抓取页面的数据
需要使用的是jsoup-1.7.3.jar包 如果需要看文档我下载请借一步到官网:http://jsoup.org/这里贴一下我用到的 Java工程的测试代码
package com.javen.Jsoup; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; publicclass JsoupTest { static String url="http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html"; /** * @param args * @throws Exception */publicstaticvoid main(String[] args) throws Exception { // TODO Auto-generated method stub BolgBody(); //test(); //Blog();/* * Document doc = Jsoup.connect("http://www.oschina.net/") * .data("query", "Java") // 请求参数 .userAgent("I ’ m jsoup") // 设置 * User-Agent .cookie("auth", "token") // 设置 cookie .timeout(3000) // * 设置连接超时时间 .post(); */// 使用 POST 方法访问 URL/* * // 从文件中加载 HTML 文档 File input = new File("D:/test.html"); Document doc * = Jsoup.parse(input,"UTF-8","http://www.oschina.net/"); */ } /** * 获取指定HTML 文档指定的body * @throws IOException */privatestaticvoid BolgBody() throws IOException { // 直接从字符串中输入 HTML 文档 String html = "<html><head><title> 开源中国社区 </title></head>" + "<body><p> 这里是 jsoup 项目的相关文章 </p></body></html>"; Document doc = Jsoup.parse(html); System.out.println(doc.body()); // 从 URL 直接加载 HTML 文档 Document doc2 = Jsoup.connect(url).get(); String title = doc2.body().toString(); System.out.println(title); } /** * 获取博客上的文章标题和链接 */publicstaticvoid article() { Document doc; try { doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postTitle"); for (Element element :ListDiv) { Elements links = element.getElementsByTag("a"); for (Element link : links) { String linkHref = link.attr("href"); String linkText = link.text().trim(); System.out.println(linkHref); System.out.println(linkText); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取指定博客文章的内容 */publicstaticvoid Blog() { Document doc; try { doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postBody"); for (Element element :ListDiv) { System.out.println(element.html()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
下面来介绍android中使用Jsoup异步解析网页的数据 请注意: 这里很容易遇到一个乱码的问题
配置文件:AndroidManifest.xml中加 权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
layout的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"><WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="200dp"/><ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/></ScrollView></LinearLayout>
主要异步加载数据的代码
package com.javen.aaa; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.webkit.WebView; import android.widget.TextView; publicclass MainActivity extends Activity { private WebView webView; private TextView textView; privatestatic final int DIALOG_KEY = 0; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webView = (WebView) findViewById(R.id.webView); textView=(TextView) findViewById(R.id.textView); try { ProgressAsyncTask asyncTask=new ProgressAsyncTask(webView,textView); asyncTask.execute(10000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 弹出"查看"对话框 @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_KEY: { ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("获取数据中 请稍候..."); dialog.setIndeterminate(true); dialog.setCancelable(true); return dialog; } } returnnull; } class ProgressAsyncTask extends AsyncTask<Integer, Integer, String> { private WebView webView; private TextView textView; public ProgressAsyncTask(WebView webView,TextView textView) { super(); this.webView=webView; this.textView=textView; } /** * 这里的Integer参数对应AsyncTask中的第一个参数 这里的String返回值对应AsyncTask的第三个参数 * 该方法并不运行在UI线程当中,主要用于异步操作,所有在该方法中不能对UI当中的空间进行设置和修改 * 但是可以调用publish Progress方法触发onProgressUpdate对UI进行操作 */ @Override protected String doInBackground(Integer... params) { String str =null; Document doc = null; try { doc = Jsoup.connect("http://www.cnblogs.com/zyw-205520/archive/2012/12/20/2826402.html").get(); Elements ListDiv = doc.getElementsByAttributeValue("class","postBody"); for (Element element :ListDiv) { str=element.html(); System.out.println(element.html()); } Log.d("doInBackground", str.toString()); System.out.println(str); //你可以试试GBK或UTF-8 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return str.toString() ; } /** * 这里的String参数对应AsyncTask中的第三个参数(也就是接收doInBackground的返回值) * 在doInBackground方法执行结束之后在运行,并且运行在UI线程当中 可以对UI空间进行设置 */ @Override protectedvoid onPostExecute(String result) { webView.loadData(result, "text/html;charset=utf-8", null); textView.setText(result); removeDialog(DIALOG_KEY); } // 该方法运行在UI线程当中,并且运行在UI线程当中 可以对UI空间进行设置 @Override protectedvoid onPreExecute() { showDialog(DIALOG_KEY); } /** * 这里的Intege参数对应AsyncTask中的第二个参数 * 在doInBackground方法当中,,每次调用publishProgress方法都会触发onProgressUpdate执行 * onProgressUpdate是在UI线程中执行,所有可以对UI空间进行操作 */ @Override protectedvoid onProgressUpdate(Integer... values) { } } }
相关文章推荐
- Angular js 学习总结
- [Falcor] Intro to JSON Graph
- JSR 303 - Bean Validation
- jsp 9个隐含对象
- JavaScript设计模式 - 单例模式
- 第24篇 js小知识和“坑”
- js table
- JS根据本月求上月与上两月,且带前导零
- JS coding thinking
- JavaScript闭包
- 一、JavaScript的输出
- WebKit之js解析和执行初始
- JS字符串对象
- 兼职面试前准备-JavaScript(1)
- ExtJS介绍与Hello World
- JS播放声音
- JS播放声音
- javascript基础之面向对象(中)
- 分享一个基于长连接+长轮询+原生的JS及AJAX实现的多人在线即时交流聊天室
- 众人口中的JAVASCRIPT