4. 윈도우 설정 : php webdriver를 활용한 크롤링
- 프로그래밍/크롤링
- 2022. 5. 16.
윈도우를 사용하면 웹브라우저(셀레늄)을 보면서 할 수 있어서 좋은것같음!
윈도우에 로컬로 webdriver 사용하기 시작!
1) 윈도우10 셀레니움 설치
- 크롬 업데이트 및 버전확인 : 크롬 설정 -> 크롬정보 -> 다시 시작 -> 버전 기억 101.0.4951.64
- 크롬 드라이버 설치 : https://www.selenium.dev/ -> download -> 스크롤 -> Browsers -> chrome -> 아까 확인한 버전이랑 앞숫자가 맞는걸로
- 셀레니움 서버 그리드 설치&실행 : 크롬드라이버 받은 사이트에서 다운로드 -> cmd -> 파일 경로로 이동 -> java -jar selenium-server-4.1.4.jar standalone
에러 발생시
- No drivers have been configured or have been found on PATH
해결방법
chromedriver.exe 환경변수 설정 후 재부팅
2) php 컴포저 설치 + php web driver
윈도우 컴포저 windows composer 설치 및 사용
1) 최신버전 다운로드 https://getcomposer.org/download/ 2) 설치 3) cmd 창에 composer 4) 안되면 환경변수 확인 환경변수 경로가 잘 설정되어있다면 재부팅 하고 다시 3번 과정 실행 4) 프로젝트 폴더로 이동..
voidfunction-e.tistory.com
3) 테스트 코드
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Cookie;
class Welcome extends CI_Controller {
public function index()
{
$host = 'http://192.168.0.14:4444/wd/hub';
$capabilities = DesiredCapabilities::chrome();
$options = new ChromeOptions();
//자동화 플래그 비활성 (봇 감지 방지)
$options->setExperimentalOption("excludeSwitches", array("enable-automation"));
//사용자 에이전트 설정
$chrome_options = ['--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36'];
$options->addArguments($chrome_options);
//브라우저가 필요없을때 headless 옵션
//$options->addArguments(['--headless','--no-sandbox']);
//옵션 적용
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
//세션 체크 필요
//$driver = RemoteWebDriver::createBySessionID($id,$host);
$driver = RemoteWebDriver::create($host, $capabilities);
//$getAllSessions = RemoteWebDriver::getAllSessions($host, 5000);
//var_dump($getAllSessions);
//echo "<hr>";
//사이트 url 입력
$driver->get('https://www.instagram.com/');
$button = $driver->findElement(
WebDriverBy::cssSelector('button div')
);
$button2 = $driver->findElement(
WebDriverBy::cssSelector('a')
);
// read text of the element and print it to output
echo "About to click to button with text: '" . $button->getText() . "'\n";
echo "<hr>".$button2->getText();
print_r($driver->getSessionID());
echo "<hr>";
}
}
참고 링크 ) 봇감지 우회
How to avoid Selenium webdriver from being detected as bot or web spider - PHP 8 Legs
Before we start to use php-webdrive and Selenium for web scraping and social media auto posting, we need to do some settings in code or file modifications to avoid our script from being detected as web bot or spider. I have listed some ways to hide our
php8legs.com
https://www.php8legs.com/en/pinterest/52-pinterest-bot-how-to-auto-login-with-php-webdriver-selenium
Pinterest bot - How to auto-login with php-webdriver + Selenium - PHP 8 Legs
Pinterest is one of the most popular social media to upload beautiful photos and website links. In the past, I used php-pinterest-bot from Seregazhuk to create pinterest boards and auto post items to Pinterest. This is a great script that covered many aspe
www.php8legs.com
'프로그래밍 > 크롤링' 카테고리의 다른 글
전송되는 post값 확인하기 (0) | 2022.05.23 |
---|---|
5. 응용법, 사용법 : php webdriver를 활용한 크롤링 (0) | 2022.05.20 |
윈도우 컴포저 windows composer 설치 및 사용 (0) | 2022.05.16 |
3. xPath사용법 : php webdriver를 활용한 크롤링 (0) | 2022.05.10 |
2. 사용법 : php webdriver를 활용한 크롤링 (0) | 2022.05.09 |