1. 程式人生 > >jsoup+HttpURLConnection+多執行緒實現編寫網路爬蟲

jsoup+HttpURLConnection+多執行緒實現編寫網路爬蟲

jsoup  HttpURLConnection  多執行緒 網路爬蟲  解析網頁內容

開發平臺:Android Studio 3.1

內容:

利用jsoup解析爬取的頁面內容

HttpURLConnectionJava的標準類,它繼承自URLConnection,可用於向指定網站傳送GET請求、POST請求。

jsoup是一款JavaHTML解析器,可直接解析某個URL地址、HTML文字內容。

見程式碼:

show2Activity.java

package com.example.administrator.test1application;

import android.app.Activity;
import 
android.os.Bundle; import android.os.Handler; import android.text.method.ScrollingMovementMethod; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import
java.io.IOException; import java.util.ArrayList; public class show2Activity extends Activity { String path; Handler handler=new Handler();//Hanlder.post(Runnable物件)方法,將Runnable post到主執行緒中執行。 TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show2
); tv=(TextView) findViewById(R.id.textView3); tv.setMovementMethod(ScrollingMovementMethod.getInstance()); final ArrayList<String> list = new ArrayList<String>(); list.add("http://www.wust.edu.cn/main.htm"); list.add("http://120.wust.edu.cn/product.html"); list.add("http://120.wust.edu.cn/product2.html"); list.add("http://202.114.242.233:8045/type/0073011401.html"); ArrayAdapter<String> adapter = new ArrayAdapter<String>( show2Activity.this, android.R.layout.simple_list_item_single_choice, list ); //Adapter資料填充器 ListView li=(ListView)findViewById(R.id.listview); li.setAdapter(adapter); li.setChoiceMode(ListView.CHOICE_MODE_SINGLE); //單選 li.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, final long id) { path=((TextView)view).getText().toString(); new Thread(new Runnable() { @Override public void run() { Document doc1 = null; try { //doc1 = Jsoup.connect("http://www.wust.edu.cn/main.htm").get(); doc1 = Jsoup.connect(path).get(); } catch (IOException e) { e.printStackTrace(); } final String title = "網頁標題: "+doc1.title(); //獲取網頁的標題 //final String s1= "頭部: "+doc1.head().toString(); final String s1= "頭部: "+doc1.head().text();//用.text()方法將網頁head標籤裡的文字內容爬取下來 // final String body= "主體內容: "+doc1.body().toString(); final String body= "主體內容: "+doc1.body().text(); //final String s2="頭部導航: "+doc1.getElementById("headtop").text(); //final String s3 = "主體內容: "+doc1.getElementsByClass("inner clearfix").text(); handler.post(new Runnable() { @Override public void run() { tv.setText("結果:"+"\n"+"\n"+title+"\n"+"\n"+"\n"+"\n"+s1+"\n"+"\n"+body); } }); } }).start(); } }); } }
activity_show2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">

        <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
    </LinearLayout>

    <TextView
android:id="@+id/textView3"
android:layout_width="400dp"
android:layout_height="100dp"
android:layout_weight="1"
android:scrollbars="vertical"
android:singleLine="false"
android:text="TextView" />

</LinearLayout>

執行截圖:



說明:listview列表可以上下拉動,內容多於長度可以顯示的長度,記得引入jsoup的依賴包,直接在新增依賴包的地方搜尋jsoup就可以搜出來,有點AS搜尋不出來就直接在build.gradle(Module:app) 下面加上這句話:

implementation 'org.jsoup:jsoup:1.11.3'
如下圖:(這個方法最實用,引入任何包都可以用這個方法)

然後,try again 或sync now就可以了。