要讀取列表標籤從url
「https://www.amazon.in/s/ref=sr_pg_2?rh=n%3A1571271031%2Cn%3A%211571272031%2Cn%3A1968024031%2Cn%3A1968093031%2Cp_n_pct-off-with-tax%3A70-%2Cp_98%3A10440597031&page=2&bbn=1968093031&ie=UTF8&qid=1501915937&lo=apparel」的UL標籤下的內容(項目名稱),你可以使用下面的代碼塊:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Q45519900_amazon_list
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://www.amazon.in/s/ref=sr_pg_2?rh=n%3A1571271031%2Cn%3A%211571272031%2Cn%3A1968024031%2Cn%3A1968093031%2Cp_n_pct-off-with-tax%3A70-%2Cp_98%3A10440597031&page=2&bbn=1968093031&ie=UTF8&qid=1501915937&lo=apparel");
List<WebElement> element = driver.findElements(By.xpath("//ul[@id='s-results-list-atf']/li[starts-with(@id, 'result_')]/div/div[@class='a-row a-spacing-none']/div/a/h2"));
System.out.println("Total items : "+element.size());
for (WebElement my_element:element)
{
String title = my_element.getAttribute("innerHTML");
System.out.println("Item name is : "+title);
}
}
}
在控制檯上的輸出是:
Total items : 48
Item name is : GlobalRang Men's Cotton Formal Shirt
Item name is : Rapphael Men's Full sleeve Casual Shirt
Item name is : Feed Up Pack of 4 Men's Pocket Shirts
Item name is : Rapphael Men's Casual Shirt
Item name is : Arihant Men's Solid Formal Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Mossimo Men's Casual Shirt
Item name is : Urban Nomad Orange Cotton Formal Shirt
Item name is : Men's Cotton Shirt
Item name is : Slub Mens Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Rapphael Men's Casual Shirt
Item name is : Arihant Men's Full Sleeves Regular fit Formal Shirt
Item name is : Arihant Men's Formal Shirt
Item name is : Rapphael Charcoal Grey & Blue color Casual Shirt for men
Item name is : Urban Nomad Yellow Cotton Formal Shirt
Item name is : GlobalRang Men's Cotton Casual Royal Blue Stand Collar Shirt
Item name is : SHADE 45 Men's Premium designed Cotton Full Sleeve Slim fit Orange color Plain…
Item name is : Basics Men's Casual Shirt
Item name is : People Men's Casual Shirt
Item name is : Dennison Men's Casual Shirt
Item name is : IZOD Men's Casual Shirt
Item name is : Deezeno Men's Cotton Shirt
Item name is : Easies Men's Casual Shirt
Item name is : Dennison Men's Formal Shirt
Item name is : Slub Men's Casual Shirt
Item name is : Scott International Men's Premium Cotton Blend Pullover Hoodie Sweatshirt With Zip…
Item name is : Rapphael Men's Full sleeve Casual Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Dennison Men's Casual Shirt
Item name is : Arihant Men's Full Sleeves Regular Fit Striped Poly Cotton Formal Shirt
Item name is : GHPC Men's 100%Cotton Slim Fit Full Sleeves Printed Casual Shirt
Item name is : Basics Men's Casual Shirt
Item name is : Rapphael Green & Black Color Casual Shirt for men
Item name is : Urban Nomad Yellow Cotton Formal Shirt
Item name is : Arihant Men's Formal Shirt
Item name is : Trendy Trotters Pink Fullsleeves Shirt
Item name is : Arihant Men's Full Sleeves Regular fit Formal Shirt
Item name is : Dennison Men's Formal Shirt
Item name is : IZOD Men's Casual Shirt
Item name is : Dennison Men's Casual Shirt
Item name is : GHPC Men's 100%Cotton Slim Fit Full Sleeves Striped Casual Shirt
Item name is : Stylox Bull Printed White Shirt [007
Item name is : Deezeno Men's Cotton Shirt
Item name is : Dennison Men's Formal Shirt
Item name is : People Men's Casual Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Rapphael Orange color Casual Shirt for men
讓我知道這個答案是否是您的問題。
歡迎來到Stack Overflow!請查看我們的[SO問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)來幫助你提出一個好問題,從而得到一個很好的答案。 –
你確定你不想通過嘗試刮擦亞馬遜搜索結果而使事情變得複雜嗎?我期望使用他們的API會更容易。 – Filburt