使用selenium的方式擷取網頁中圖片的連結和網頁的連結,來判斷是否是死鏈(二)

來源:互聯網
上載者:User

標籤:取圖   pat   bsp   code   htm   getc   str   images   element   

上一篇使用JavaRegex來判斷和擷取圖片的連結以及跳轉的網址,這篇使用selenium的內建的API(getAttribute)來擷取網頁中指定的內容

實現內容:擷取下面所有圖片的連結地址以及跳轉地址,使用get請求判斷是否有死鏈

頁面內容

頁面的源碼,需要擷取頁面的href後的地址,以及src後的地址,:

代碼實現可以看出圖片都在一個div中,實現的思想為:擷取控制項集合,在擷取每一個li下的元素,在擷取,在取出資料下的屬性名稱的屬性值

public void new_classification() throws Exception {        op.loopGet(home, 40, 3, 60);        op.loopClickElement("swimmer", 3, 10, explicitWaitTimeoutLoop);// 進入到某個頁面        if (driver.getCurrentUrl().contains("swimwear")) {            List<WebElement> newimage = driver.findElements(By.xpath("//*[@id=‘js_proList‘]/ul/li"));// 圖片的控制項集合            for (int i = 0; i < newimage.size(); i++) {                String contentURL = newimage.get(i).findElement(By.xpath("p[1]/a[1]")).getAttribute("href");// 圖片的跳轉地址                String imageURL = newimage.get(i).findElement(By.xpath("p[1]/a[1]/img")).getAttribute("src");// 圖片的連結地址                Pub.get(contentURL);                System.out.println("**********************");                Pub.get(imageURL);//get請求            }        } else {            Log.logError("沒有進入到new頁面");        }    }
結果展示

 

 如果需要使用Regex,查看文章:http://www.cnblogs.com/chongyou/p/7286447.html

使用selenium的方式擷取網頁中圖片的連結和網頁的連結,來判斷是否是死鏈(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.