日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过java获取抖音用户主页信息(2020年9月)

發(fā)布時間:2024/3/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过java获取抖音用户主页信息(2020年9月) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過java獲取抖音用戶主頁信息(2020年9月)

環(huán)境準備

本篇文章基于sts編輯器,jdk1.8,maven3

項目結(jié)構(gòu)如下圖

執(zhí)行結(jié)果如下圖

html請求工具類

HttpUtils代碼如下

package test1;import org.jsoup.Jsoup; import org.jsoup.nodes.Document;import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage;/*** <pre>* 使用net.sourceforge.htmlunit獲取完整的html頁面,即完成后臺js代碼的運行* </pre>*/ public class HttpUtils {/*** 請求超時時間,默認30秒*/private int timeout = 30000;/*** 等待異步JS執(zhí)行時間,默認20秒*/private int waitForBackgroundJavaScript = 20000;private static HttpUtils httpUtils;private HttpUtils() {}public static HttpUtils getInstance() {if (httpUtils == null)httpUtils = new HttpUtils();return httpUtils;}public int getTimeout() {return timeout;}/*** 請求超時時間** @param timeout*/public void setTimeout(int timeout) {this.timeout = timeout;}public int getWaitForBackgroundJavaScript() {return waitForBackgroundJavaScript;}/*** 設(shè)置獲取完整HTML頁面時等待異步JS執(zhí)行的時間*/public void setWaitForBackgroundJavaScript(int waitForBackgroundJavaScript) {this.waitForBackgroundJavaScript = waitForBackgroundJavaScript;}/*** 將網(wǎng)頁返回為解析后的文檔格式*/public static Document parseHtmlToDoc(String html) throws Exception {return removeHtmlSpace(html);}private static Document removeHtmlSpace(String str) {Document doc = Jsoup.parse(str);String result = doc.html().replace("&nbsp;", "");return Jsoup.parse(result);}/*** 獲取頁面文檔字符串(等待異步JS執(zhí)行)*/public String getHtmlPageResponse(String url) throws Exception {String result = null;final WebClient webClient = new WebClient(BrowserVersion.CHROME);webClient.getOptions().setThrowExceptionOnScriptError(false);//當JS執(zhí)行出錯的時候是否拋出異常webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//當HTTP的狀態(tài)非200時是否拋出異常webClient.getOptions().setActiveXNative(false);webClient.getOptions().setCssEnabled(false);//是否啟用CSSwebClient.getOptions().setJavaScriptEnabled(true); //非常重要,啟用JS,適用于頁面加載后異步調(diào)用jswebClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要,設(shè)置支持AJAXwebClient.getOptions().setTimeout(timeout);//設(shè)置的請求超時時間webClient.setJavaScriptTimeout(timeout);//設(shè)置JS執(zhí)行的超時時間HtmlPage page;try {page = webClient.getPage(url);} catch (Exception e) {webClient.close();throw e;}webClient.waitForBackgroundJavaScript(waitForBackgroundJavaScript);//方法阻塞線程result = page.asXml();webClient.close();return result;}/*** 獲取頁面文檔Document對象(等待異步JS執(zhí)行)*/public Document getHtmlPageResponseAsDocument(String url) throws Exception {return parseHtmlToDoc(getHtmlPageResponse(url));} }

通過junit的測試類

package test1;import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.junit.Test;public class HttpUtilsTest {private static final String TEST_URL = "用戶主頁url";@Testpublic void testGetHtmlPageResponseAsDocument() {HttpUtils httpUtils = HttpUtils.getInstance();httpUtils.setTimeout(30000);httpUtils.setWaitForBackgroundJavaScript(30000);try {Document document = httpUtils.getHtmlPageResponseAsDocument(TEST_URL);//TODO//System.out.println(document);Element element = document.getElementById("pagelet-user-info");//獲取元素節(jié)點等//System.out.println(element);System.out.println("頭像url:"+element.getElementsByTag("img").attr("src"));System.out.println("昵稱:"+element.getElementsByTag("p").get(0).text());System.out.println(element.getElementsByTag("p").get(1).text());System.out.println("簽名:"+element.getElementsByTag("p").get(2).text());System.out.println("關(guān)注:"+element.getElementsByTag("p").get(3).getElementsByAttributeValue("class", "num").get(0).text());System.out.println("粉絲:"+element.getElementsByTag("p").get(3).getElementsByAttributeValue("class", "num").get(1).text());System.out.println("贊:"+element.getElementsByTag("p").get(3).getElementsByAttributeValue("class", "num").get(2).text());System.out.println("作品:"+element.getElementsByAttributeValue("class", "video-tab").first().getElementsByAttributeValue("class", "num").get(0).text());System.out.println("喜歡:"+element.getElementsByAttributeValue("class", "video-tab").first().getElementsByAttributeValue("class", "num").get(1).text());} catch (Exception e) {e.printStackTrace();}}}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.test1</groupId><artifactId>test1</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>net.sourceforge.htmlunit</groupId><artifactId>htmlunit</artifactId><version>2.27</version></dependency><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.8.3</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version> </dependency> </dependencies> </project>

以上為實現(xiàn)獲取抖音主頁用戶信息的全部內(nèi)容,隨著抖音的不斷更新,代碼需要及時調(diào)整。本文會持續(xù)更新。初來乍到,不足之處還請大神指教。謝謝

總結(jié)

以上是生活随笔為你收集整理的通过java获取抖音用户主页信息(2020年9月)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。