生活随笔
收集整理的這篇文章主要介紹了
爬取虎牙之三:通过json数据获取所有直播情况
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
動態ajax頁面為什么會再同一個頁面有多個分頁,就是因為當你點擊其他頁的時候服務器返回一串json串,js執行json參數達到改變頁面效果。下面說說如何抓取json串。
1:爬取信息要對源頁有所了解,首先打開虎牙直播,谷歌F12選取network。點xhr。里面可能會有多個json文件,點擊第三頁。
發現點擊第三頁后會出現一個新的json串。點擊看下
會發現服務器返回的數據就是這個頁面所顯示的。你想要的數據都在里面。可以通過這個數據爬取有用的信息。查看服務器的請求
最下面一行的Query String Parameters就是需要的請求,這樣我們模擬請求就可以獲取信息。
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;public class test2 {public static void main(String[] args) throws IOException{String url="https://www.huya.com/";Connection connect = Jsoup.connect(url).timeout(20000);Map header = new HashMap(); header.put("m", "LiveList");header.put("do", "getLiveListByPage");header.put("tagAll", "0"); header.put("page", "3"); connect.data(header);Document doc=connect.get();System.out.println(doc.text());}
}
卻發現輸出的是
這個就是前面xhr中的response文件。并且這是一個json串,要想轉換json變成我們想要的東西。就要使用阿里的fastjson解析工具,導入fastjson包就可以使用。對于fastjson的簡單使用,可以先簡單對單個數據摸索。嘗試看看。就可以獲得想要的數據。json串的解析和獲取信息需要自己慢慢摸索。貼上最終的代碼:
package 虎牙斗魚直播平臺
;import java
.io
.IOException
;
import java
.sql
.DriverManager
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.util
.HashMap
;
import java
.util
.Map
;import org
.jsoup
.Connection
;
import org
.jsoup
.Jsoup
;
import org
.jsoup
.nodes
.Document
;
import org
.jsoup
.select
.Elements
;import com
.alibaba
.fastjson
.JSON
;
import com
.alibaba
.fastjson
.JSONArray
;
import com
.alibaba
.fastjson
.JSONObject
;
public class 抓取所有主播
{public static void main(String
[] args
) throws IOException
, SQLException
, ClassNotFoundException
{int id
= 1;java
.sql
.Connection con
;Class
.forName("com.mysql.jdbc.Driver");System
.out
.println("數據庫驅動加載成功");con
= DriverManager
.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/db?useSSL=true", "root", "123456");System
.out
.println("數據庫連接成功");String url
= "https://www.huya.com/";Connection connect
= Jsoup
.connect(url
).timeout(20000);Map
<String, String> header
= new HashMap<String, String>();header
.put("m", "LiveList");header
.put("do", "getLiveListByPage");header
.put("tagAll", "0");int page
= 0;Document exam
= Jsoup
.connect("https://www.huya.com/l").get();Elements links
= exam
.getElementsByClass("list-page");page
= Integer
.parseInt(links
.attr("data-pages"));System
.out
.println(page
);for (int i
= 0; i
< page
; i
++) {header
.put("page", 1 + i
+ "");Connection data
= connect
.data(header
);Document doc
= data
.get(); try {JSONObject jsonObj
= JSON
.parseObject(doc
.text()); JSONObject jsonOb
= JSON
.parseObject(jsonObj
.getString("data"));JSONArray jarr
= jsonOb
.getJSONArray("datas");for (Object jar
: jarr
) {JSONObject js
= JSON
.parseObject(jar
.toString());int renshu
= Integer
.parseInt((String
) js
.get("totalCount"));String type
= js
.get("gameFullName").toString();String sql2
= "insert into satuday(name,href,number,type)value(?,?,?,?)";PreparedStatement pstmt
= con
.prepareStatement(sql2
);pstmt
.setString(1, (String
) js
.get("nick")); pstmt
.setString(2, "https://www.huya.com/" + js
.get("profileRoom"));pstmt
.setInt(3, renshu
);pstmt
.setString(4, type
);pstmt
.execute();System
.out
.println(js
.get("gameFullName") + "" + id
++ + " " + i
+ "");}} catch (Exception e
) {System
.out
.println(e
.getMessage());}}con
.close();}
}
- 如果對爬蟲,數據結構,java感興趣可以關注筆者公眾號:bigsai
總結
以上是生活随笔為你收集整理的爬取虎牙之三:通过json数据获取所有直播情况的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。