"자바 크롤링"의 두 판 사이의 차이
DB CAFE
(→크롤링 툴) |
(→크롤링 샘플) |
||
14번째 줄: | 14번째 줄: | ||
=== 셀레이엄 === | === 셀레이엄 === | ||
https://heodolf.tistory.com/103?category=887835 | https://heodolf.tistory.com/103?category=887835 | ||
+ | |||
+ | ==== 트위터 크롤링 ==== | ||
+ | <source lang=java> | ||
+ | // App.java | ||
+ | |||
+ | package selenium; | ||
+ | |||
+ | import java.nio.file.Path; | ||
+ | import java.nio.file.Paths; | ||
+ | import java.util.List; | ||
+ | import java.util.Properties; | ||
+ | |||
+ | import org.openqa.selenium.By; | ||
+ | import org.openqa.selenium.NoSuchElementException; | ||
+ | import org.openqa.selenium.TimeoutException; | ||
+ | import org.openqa.selenium.WebElement; | ||
+ | import org.openqa.selenium.chrome.ChromeDriver; | ||
+ | import org.openqa.selenium.chrome.ChromeOptions; | ||
+ | import org.openqa.selenium.support.ui.ExpectedConditions; | ||
+ | import org.openqa.selenium.support.ui.WebDriverWait; | ||
+ | |||
+ | public class App { | ||
+ | |||
+ | // 중략 | ||
+ | |||
+ | public static void runSelenium(String URL) throws Exception { | ||
+ | // 1. WebDriver 경로 설정 | ||
+ | Path path = Paths.get(System.getProperty("user.dir"), "src/main/resources/chromedriver.exe"); | ||
+ | System.setProperty("webdriver.chrome.driver", path.toString()); | ||
+ | |||
+ | // 2. WebDriver 옵션 설정 | ||
+ | ChromeOptions options = new ChromeOptions(); | ||
+ | options.addArguments("--start-maximized"); // 최대크기로 | ||
+ | options.addArguments("--headless"); // Browser를 띄우지 않음 | ||
+ | options.addArguments("--disable-gpu"); // GPU를 사용하지 않음, Linux에서 headless를 사용하는 경우 필요함. | ||
+ | options.addArguments("--no-sandbox"); // Sandbox 프로세스를 사용하지 않음, Linux에서 headless를 사용하는 경우 필요함. | ||
+ | |||
+ | // 3. WebDriver 객체 생성 | ||
+ | ChromeDriver driver = new ChromeDriver( options ); | ||
+ | |||
+ | // 4. 웹페이지 요청 | ||
+ | driver.get(URL); | ||
+ | |||
+ | // 5. HTML 저장. | ||
+ | saveHtml("twitter-selenium.html", driver.getPageSource() ); | ||
+ | |||
+ | try { | ||
+ | // 6. 트윗 목록 Block 조회, 로드될 때까지 최대 30초간 대기 | ||
+ | WebDriverWait wait = new WebDriverWait(driver, 30); | ||
+ | WebElement parent = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("section[aria-labelledby*=\"accessible-list\"]"))); | ||
+ | |||
+ | // 7. 트윗 콘텐츠 조회 | ||
+ | List<WebElement> contents = parent.findElements(By.cssSelector("div.css-1dbjc4n.r-my5ep6.r-qklmqi.r-1adg3ll")); | ||
+ | System.out.println( "조회된 콘텐츠 수 : "+contents.size() ); | ||
+ | |||
+ | if( contents.size() > 0 ) { | ||
+ | // 8. 트윗 상세 내용 탐색 | ||
+ | for(WebElement content : contents ) { | ||
+ | try { | ||
+ | String username = content.findElement(By.cssSelector("span > span.css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r-bcqeeo.r-qvutc0")).getText(); | ||
+ | String id = content.findElement(By.cssSelector("span.css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r-bcqeeo.r-qvutc0")).getText(); | ||
+ | String text = content.findElement(By.cssSelector("div.css-901oao.r-hkyrab.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0")).getText(); | ||
+ | |||
+ | System.out.println( "========================" ); | ||
+ | System.out.println( username+" "+id ); | ||
+ | System.out.println( text ); | ||
+ | System.out.println( "========================" ); | ||
+ | } catch ( NoSuchElementException e ) { | ||
+ | // pass | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } catch ( TimeoutException e ) { | ||
+ | System.out.println("목록을 찾을 수 없습니다."); | ||
+ | } finally { | ||
+ | // 9. HTML 저장. | ||
+ | saveHtml("twitter-selenium-loaded.html", driver.getPageSource() ); | ||
+ | } | ||
+ | |||
+ | // WebDriver 종료 | ||
+ | driver.quit(); | ||
+ | } | ||
+ | |||
+ | // 중략 | ||
+ | } | ||
+ | |||
+ | </source> | ||
== 크롤링 툴 == | == 크롤링 툴 == |
2021년 4월 5일 (월) 00:23 판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
목차
1 자바 크롤링[편집]
1.1 자바 설치[편집]
1.2 자바 크롤링 라이브러리[편집]
https://mobilemonitoringsolutions.com/8-most-popular-java-web-crawling-scraping-libraries/
1.3 크롤링 샘플[편집]
1.3.1 jsoup[편집]
1.3.2 셀레이엄[편집]
https://heodolf.tistory.com/103?category=887835
1.3.2.1 트위터 크롤링[편집]
// App.java
package selenium;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class App {
// 중략
public static void runSelenium(String URL) throws Exception {
// 1. WebDriver 경로 설정
Path path = Paths.get(System.getProperty("user.dir"), "src/main/resources/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", path.toString());
// 2. WebDriver 옵션 설정
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized"); // 최대크기로
options.addArguments("--headless"); // Browser를 띄우지 않음
options.addArguments("--disable-gpu"); // GPU를 사용하지 않음, Linux에서 headless를 사용하는 경우 필요함.
options.addArguments("--no-sandbox"); // Sandbox 프로세스를 사용하지 않음, Linux에서 headless를 사용하는 경우 필요함.
// 3. WebDriver 객체 생성
ChromeDriver driver = new ChromeDriver( options );
// 4. 웹페이지 요청
driver.get(URL);
// 5. HTML 저장.
saveHtml("twitter-selenium.html", driver.getPageSource() );
try {
// 6. 트윗 목록 Block 조회, 로드될 때까지 최대 30초간 대기
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement parent = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("section[aria-labelledby*=\"accessible-list\"]")));
// 7. 트윗 콘텐츠 조회
List<WebElement> contents = parent.findElements(By.cssSelector("div.css-1dbjc4n.r-my5ep6.r-qklmqi.r-1adg3ll"));
System.out.println( "조회된 콘텐츠 수 : "+contents.size() );
if( contents.size() > 0 ) {
// 8. 트윗 상세 내용 탐색
for(WebElement content : contents ) {
try {
String username = content.findElement(By.cssSelector("span > span.css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r-bcqeeo.r-qvutc0")).getText();
String id = content.findElement(By.cssSelector("span.css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r-bcqeeo.r-qvutc0")).getText();
String text = content.findElement(By.cssSelector("div.css-901oao.r-hkyrab.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0")).getText();
System.out.println( "========================" );
System.out.println( username+" "+id );
System.out.println( text );
System.out.println( "========================" );
} catch ( NoSuchElementException e ) {
// pass
}
}
}
} catch ( TimeoutException e ) {
System.out.println("목록을 찾을 수 없습니다.");
} finally {
// 9. HTML 저장.
saveHtml("twitter-selenium-loaded.html", driver.getPageSource() );
}
// WebDriver 종료
driver.quit();
}
// 중략
}