天天看點

selenium-java 解決 jsoup遇到javascript重定向無法擷取内容的問題

問題

通過jsoup爬網站的時候,遇到javascript重定向,遞歸無果,查了許多資料都無法解決

在這裡插入代碼片

selenium-java 解決 jsoup遇到javascript重定向無法擷取内容的問題
利用selenium龜速實作,先解決問題

maven依賴

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.141.59</version>
            </dependency>
           

下載下傳webdriver驅動,我用的是chrome

http://chromedriver.storage.googleapis.com/index.html

String url = "";
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(url);
           
遇到坑爹的jar包沖突 java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V

這位大佬解決了我的問題

https://blog.csdn.net/qq_27948811/article/details/100047242

和我的swagger沖突了

<dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
           

回到Jsoup

String pageSource = driver.getPageSource();
 Document document = Jsoup.parse(pageSource);
           

後面就可以繼續用jsoup解決了,不過很慢,中間會彈一次浏覽器,感覺很難受

如果哪位大神有好的辦法可以解決javascript的重定向,歡迎交流~~~~~~~