[問題] JSOUP抓資料時找不到POST參數

看板java作者 (屌面)時間9年前 (2015/09/24 14:30), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
問個Joup抓google play網頁時遇到的問題,我想抓使用者曾經安裝過的app資料與這些 app的評分 ---------------------------------------------------------- 首先我參考 http://www.mkyong.com/java/how-to-automate-login-a-website-java-example/ 自動登入使用者的gmail帳號 ---------------------------------------------------------- 接著我用以下程式碼只抓到了前19支app的資料 Document doc = (Document) Jsoup.connect("https://play.google.com/apps").get(); Elements ids = doc.select("a.title"); for(int i = 0; i < ids.size(); i++) { System.out.println(ids.get(i).ownText()); System.out.println("-------------------------------------"); } ---------------------------------------------------------- 於是我就用firefox觀察什麼時後載入後面的app,發現是如附圖所示的 POST時載入的,POST其中有兩個參 pageTok=CgQIARAU:S:ANO1ljIHHao & token=oRGw3hsIH91sA88DIpRqvLkbTAQ:1443001966556 是我用jsoup發POST前我必須要先知道的。 --------------------------------------------------------- 參數pagTok我從 Document doc = (Document) Jsoup.connect("https://play.google.com/apps").get(); 這行回傳的html裡面的某個<script>中的變數中找到了,但token這個參數我找很久都找不到... --------------------------------------------------------- 我觀察到token每次POST都不一樣,但隨便用其中一個token都可以成功抓取所有app,但 每個使用者的token都不一樣,所以我還是必須知道如何取得token這個參數。 --------------------------------------------------------- 以下是我改寫後的程式碼,此程式碼可以抓完我自己所有曾安裝的app,不知道有沒有高 手處理過這種問題,或是有其他方法能抓到我需要的資料,感謝各位~~~ int count=0; Document doc = (Document) Jsoup.connect("https://play.google.com/apps").get(); Elements ids = doc.select("a.title"); Elements scripts = doc.select("script"); String pagTok = scripts.get(12).toString().split("\\\\42")[3]; String token="oRGw3hsIH91sA88DIpRqvLkbTAQ:1443001966556"; for(int i = 0; i < ids.size(); i++) { System.out.println(ids.get(i).ownText()); Document document = (Document) Jsoup.connect("https://play.google.com/store/apps/details?id="+ids.get(i).attr("href").split("=")[1]).get(); Elements pkg = document.select("div.review-stars-container"); System.out.println(pkg.select("div.tiny-star").attr("aria-label")); System.out.println("-------------------------------------"); count++; } while (true){ Document doc1 = (Document) Jsoup.connect("https://play.google.com/apps?pagTok=" + pagTok + "&token="+token).post(); Elements ids1 = doc1.select("a.title"); scripts = doc1.select("script"); for(int i=0; i < ids1.size(); i++) { System.out.println(ids1.get(i).ownText()); Document document = (Document) Jsoup.connect("https://play.google.com/store/apps/details?id=" + ids1.get(i).attr("href").split("=")[1]).get(); Elements pkg = document.select("div.review-stars-container"); System.out.println(pkg.select("div.tiny-star").attr("aria-label")); System.out.println("-------------------------------------"); count++; } if (scripts.get(12).toString().split("\\\\42").length==1) break; pagTok = scripts.get(12).toString().split("\\\\42")[3].replace("\\\\u003d", "%3D"); } } 首次發問,排版有點亂,請各位見諒~~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.121.197.131 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1443076254.A.354.html

09/25 12:33, , 1F
pastebin 是你的好朋友
09/25 12:33, 1F
文章代碼(AID): #1M0vYUDK (java)
文章代碼(AID): #1M0vYUDK (java)