获取重定向到另一个网页的网页内容
Get content of a webpage which is redirected to another webpage
我要抓取网页内容 - http://www.pgmfi.org/. But if we visit the page, we will see it redirects to the page - 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 正文的情况下获取页面重定向到的 url 吗?
我更喜欢使用 crawler4j
的解决方案。即使 jsoup
中的解决方案对我来说也很好。
crawler4j
不支持从 meta-refresh
中提取 URL。但是,crawler4j
提供了相应的元标记(参见 HTMLParseData
),因此您可以增强 visit(...)
方法以将提取的 URL 添加到 Frontier
对象在 WebCrawler
通过 schedule(...)
.
但是,Frontier
在 WebCrawler
中只有 private
访问权限,因此不能用于您的具体子 class。要修改它,您需要 (a) 分叉它或 (b) 使用反射 API 更改访问修饰符。
另一种方法是在官方问题跟踪器上提出问题 here。
我要抓取网页内容 - http://www.pgmfi.org/. But if we visit the page, we will see it redirects to the page - 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 正文的情况下获取页面重定向到的 url 吗?
我更喜欢使用 crawler4j
的解决方案。即使 jsoup
中的解决方案对我来说也很好。
crawler4j
不支持从 meta-refresh
中提取 URL。但是,crawler4j
提供了相应的元标记(参见 HTMLParseData
),因此您可以增强 visit(...)
方法以将提取的 URL 添加到 Frontier
对象在 WebCrawler
通过 schedule(...)
.
但是,Frontier
在 WebCrawler
中只有 private
访问权限,因此不能用于您的具体子 class。要修改它,您需要 (a) 分叉它或 (b) 使用反射 API 更改访问修饰符。
另一种方法是在官方问题跟踪器上提出问题 here。