我想抓取網頁的內容 - http://www.pgmfi.org/。但是如果我們訪問該頁面,我們會看到它重定向到頁面 - http://twiki.pgmfi.org/bin/view。獲取重定向到另一個網頁的網頁內容
當我嘗試使用jsoup
或crawler4j
從URL(http://www.pgmfi.org/)抓取內容時,我得到以下內容。
Looking for PGMFI.ORG Home ? Please wait redirecting to: http://twiki.pgmfi.org
但我想獲得從重定向網頁(http://twiki.pgmfi.org/bin/view)網頁的更多信息。當我運行一個簡單的代碼使用jsoup
時,我發現了以下內容。
String url = "http://www.pgmfi.org/";
Response response = Jsoup.connect(url).followRedirects(false).execute();
System.out.println(response.statusCode() + " : " + response.url());
//check if URL is redirect?
System.out.println("Is URL going to redirect : " + response.hasHeader("location"));
System.out.println("Target : " + response.header("location"));
輸出:
200 : http://www.pgmfi.org/
Is URL going to redirect : false
Target : null
所以,重定向顯然不是直線前進。我的問題 - 有什麼辦法,我可以得到頁面重定向到的網址而不解析html正文?
我更喜歡使用crawler4j
的解決方案。即使在jsoup
的解決方案對我來說也不錯。
您需要弄清楚您的scraping庫是否支持基於元刷新的重定向。如果沒有,請自行解析出來或者從http://twiki.pgmfi.org開始抓取。 – pvg
我不知道爲什麼我會倒下。我在SO上提出這個問題之前嘗試了不同的選擇,但失敗了。我需要從原始網址抓取重定向的網頁,因此我現在不考慮直接從重定向的網址抓取。 –
這是不是很清楚你問什麼。也許你不熟悉重定向的類型?嚴格來說,它不是重定向,而是依賴於解析HTML。 – pvg