Prompt Title: Scraping Web Data with Java

Created 1 year ago
Votes 0
Views 251 times
IT
0
Human: Take a Developer's Persona and write a JAVA Selenium Automation Script to scrape name, designation, email and phone from webpage link and store it in excelsheet. take webpage link, field's xpath from excelsheet
ChatGPT:

Sure, here's a Java Selenium automation script that can scrape the name, designation, email, and phone from a webpage link and store it in an Excel sheet:

java
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ScrapeWebpage { public static void main(String[] args) { String webpageLink = "https://example.com"; // Replace with your webpage link String excelFilePath = "output.xlsx"; // Replace with your output Excel file path String sheetName = "Data"; // Replace with your sheet name String nameXpath = "//div[@class='name']"; // Replace with the Xpath of the name field String designationXpath = "//div[@class='designation']"; // Replace with the Xpath of the designation field String emailXpath = "//div[@class='email']"; // Replace with the Xpath of the email field String phoneXpath = "//div[@class='phone']"; // Replace with the Xpath of the phone field System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); // Replace with your ChromeDriver path WebDriver driver = new ChromeDriver(); driver.get(webpageLink); List<String> names = new ArrayList<>(); List<String> designations = new ArrayList<>(); List<String> emails = new ArrayList<>(); List<String> phones = new ArrayList<>(); List<WebElement> nameElements = driver.findElements(By.xpath(nameXpath)); for (WebElement nameElement : nameElements) { names.add(nameElement.getText()); } List<WebElement> designationElements = driver.findElements(By.xpath(designationXpath)); for (WebElement designationElement : designationElements) { designations.add(designationElement.getText()); } List<WebElement> emailElements = driver.findElements(By.xpath(emailXpath)); for (WebElement emailElement : emailElements) { emails.add(emailElement.getText()); } List<WebElement> phoneElements = driver.findElements(By.xpath(phoneXpath)); for (WebElement phoneElement : phoneElements) { phones.add(phoneElement.getText()); } driver.quit(); Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(sheetName); int rowNum = 0; Row headerRow = sheet.createRow(rowNum++); headerRow.createCell(0).setCellValue("Name"); headerRow.createCell(1).setCellValue("Designation"); headerRow.createCell(2).setCellValue("Email"); headerRow.createCell(3).setCellValue("Phone"); for (int i = 0; i < names.size(); i++) { Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(names.get(i)); row.createCell(1).setCellValue(designations.get(i)); row.createCell(2).setCellValue(emails.get(i)); row.createCell(3).setCellValue(phones.get(i)); } try { FileOutputStream outputStream = new FileOutputStream(new File(excelFilePath)); workbook.write(outputStream); workbook.close(); System.out.println("Data has been written to " + excelFilePath); } catch (IOException e) { e.printStackTrace(); } } }

Make sure to replace the webpageLink, excelFilePath, sheetName, nameXpath, `designation

avatar
Abhishek Vk
Shared 2 prompts
Created 1 year ago

Leave a Comment

Related Tag Prompts