我有网址 https://www.facebook.com/ads/library/?id=286238429359299
被重定向到 https://www.facebook.com/ads/library/?active_status=all&ad_type=political_and_issue_ads&country=US&impression_search_field=has_impressions_lifetime&id=286238429359299&view_all_page_id=575939395898200
在浏览器中。
我正在使用以下代码:
@Test
public void createWebClient() throws IOException {
getLogger("com.gargoylesoftware").setLevel(OFF);
WebClient webClient = new WebClient(CHROME);
WebClientOptions options = webClient.getOptions();
options.setJavaScriptEnabled(true);
options.setRedirectEnabled(true);
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
// IMPORTANT: Without the country/language selection cookie the redirection does not work!
URL s = webClient.getPage("https://www.facebook.com/ads/library/?id=286238429359299").getUrl();
}
上面的代码没有考虑重定向,有什么我遗漏的吗?我需要获取原始 URL 解析到的最终 URL。
最佳答案
实际上是网址 https://www.facebook.com/ads/library/?id=286238429359299返回一个带有javascript的页面。javascript会检测web浏览器的环境。例如js会检测当前浏览器是否为Headless浏览器,web驱动是否合法。所以我认为解决方案是分析javascript和您将获得最终网址。
关于java - 如何使用 HtmlUnit 获得 URL 的最终重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63000800/