修改 for 循环,爬取所有信息:public static void main(String[] args){ System.setProperty("webdriver.firefox.bin","C:\Users\Y\Desktop\软件测试\实验二\实验相关\FirefoxChina_13.0.1\Mozilla Firefox\firefox.exe"); WebDriver driver = new FirefoxDriver(); Navigation na = driver.navigate(); na.to("https://shz.58.com/ershoufang/"); driver.findElement(By.id("search-input")).clear(); List<WebElement> ele = driver.findElements(By.cssSelector("h3.property-content-title-name"));//爬取房屋名字 List<WebElement> ele1s=new ArrayList<>(); List<WebElement> ele3s=new ArrayList<>(); for(int j=1;j<=10;j++){ String s1="//div[@id='__layout']/div/section/section[3]/section/section[2]/div["+j+ "]/a/div[2]/div/section/div[@class='property-content-info']/p[2]"; String s3="//div[@id='__layout']/div/section/section[3]/section/section[2]/div["+j+ "]/a/div[2]/div/section/div[@class='property-content-info property-content-info-comm']/p[2]"; WebElement ele11 = driver.findElement(By.xpath(s1));//爬取房屋面积信息 WebElement ele33 = driver.findElement(By.xpath(s3));//爬取房屋地址信息 ele1s.add(ele11); ele3s.add(ele33); } List<WebElement> ele2 = driver.findElements(By.cssSelector("p.property-price-total"));//爬取房屋价格信息 for(int i=0;i<ele1s.size();i++){ System.out.println("名字:"+ele.get(i).getText()); System.out.println("地址:"+ele3s.get(i).getText()); System.out.println("面积:"+ele1s.get(i).getText()); System.out.println("价格:"+ele2.get(i).getText()); System.out.println(); } }
时间: 2023-06-18 22:06:03 浏览: 183
public static void main(String[] args){
System.setProperty("webdriver.firefox.bin","C:\\Users\\Y\\Desktop\\软件测试\\实验二\\实验相关\\FirefoxChina_13.0.1\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation na = driver.navigate();
na.to("https://shz.58.com/ershoufang/");
driver.findElement(By.id("search-input")).clear();
List<WebElement> ele = driver.findElements(By.cssSelector("h3.property-content-title-name"));//爬取房屋名字
List<WebElement> ele1s=new ArrayList<>();
List<WebElement> ele3s=new ArrayList<>();
List<WebElement> ele2 = driver.findElements(By.cssSelector("p.property-price-total"));//爬取房屋价格信息
for(int j=1;j<=10;j++){
String s1="//div[@id='__layout']/div/section/section[3]/section/section[2]/div["+j+ "]/a/div[2]/div/section/div[@class='property-content-info']/p[2]";//爬取房屋面积信息
String s3="//div[@id='__layout']/div/section/section[3]/section/section[2]/div["+j+ "]/a/div[2]/div/section/div[@class='property-content-info property-content-info-comm']/p[2]";//爬取房屋地址信息
WebElement ele11 = driver.findElement(By.xpath(s1));
WebElement ele33 = driver.findElement(By.xpath(s3));
ele1s.add(ele11);
ele3s.add(ele33);
}
for(int i=0;i<ele.size();i++){
System.out.println("名字:"+ele.get(i).getText());
System.out.println("地址:"+ele3s.get(i).getText());
System.out.println("面积:"+ele1s.get(i).getText());
System.out.println("价格:"+ele2.get(i).getText());
System.out.println();
}
}
阅读全文