为每句代码做注释,并给出优化算法: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 16:06:08 浏览: 199
//设置Firefox浏览器的路径
System.setProperty("webdriver.firefox.bin","C:\Users\Y\Desktop\软件测试\实验二\实验相关\FirefoxChina_13.0.1\Mozilla Firefox\firefox.exe");
//创建FirefoxDriver对象
WebDriver driver = new FirefoxDriver();
//创建Navigation对象
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"));
//创建空的WebElement列表
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();
}
优化算法:
1.使用显式等待代替线程睡眠,提高代码执行效率。
2.使用ChromeDriver代替FirefoxDriver,ChromeDriver比FirefoxDriver更快。
3.使用多线程技术,同时爬取多个页面,提高爬取效率。
阅读全文