저번글에서 말했듯이 indeed.com에 접속을 해준다.
접속한 후에 검색어란에 python이라고 쳐보면 python직종이 다나온다
그렇다면 url을 분석해보면
https://kr.indeed.com/jobs?q=python 으로 jobs?q= 뒤에 python이 오게되는 것을 알 수 있다.
default값인 url을
from requests import get
#pip install requests
from bs4 import BeautifulSoup
from extractiors.wwr import extract_wwr_jobs
base_url ="https://kr.indeed.com/jobs?q="
search_term = "python"
response = get(f"{base_url}{search_term}")
if response.status_code != 200:
print("cant")
else:
print(response.text)
이렇게 집어 넣는다면 바로 Access denied가 뜰 것이다. 바로 indeed.com에서 우리가 봇인지 사람인지 check를 하는 것이다. 그래서 이럴때 쓰는 것이 바로 Selenium이다.
Selenium은 직접 방문을 하여 봇이 아니라고 생각하게 만드는 것이다.
우선 selenium을 설치해보자
pip install selenium
그리고 크롬접속->오른쪽 상단 점3개 클릭 -> 도움말 -> 크롬 정보 클릭하면
이렇게 버전이 나온다 앞의 버전을 보고
https://chromedriver.chromium.org/downloads
ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 109, please download ChromeDriver 109.0.5414.25 If you are using Chrome version 108, please download ChromeDriver 108.0.5359.71 If you are using Chrome version 107, please download ChromeDriver 107.0.5304.62
chromedriver.chromium.org
접속 후 자신의 크롬 정보에 맞게 다운로드한다.
그 드라이버 파일을 py파일과 동일한 위치에 넣어준다.
그리고
hello.py로 돌아가서
from requests import get
#pip install requests
from bs4 import BeautifulSoup
from extractiors.wwr import extract_wwr_jobs
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
browser = webdriver.Chrome(options=options)
#크롬 브라우저 생성
browser.get("https://kr.indeed.com/jobs?q=python&limit=50")
#브라우저에게 이 페이지를 방문해달라고 부탁
print(browser.page_source)
그리고 실행을 시키면 브라우저를 열어주고 indeed.com으로 이동해주었다.
하지만 바로 자동으로 꺼지는 현상이 나타났다.
만약 꺼지지 않는다면 더보기를 누르지 않고 진행 바란다.
https://goddino.tistory.com/353
[py] 셀레니움 기본 설정
셀레니움 작업시 필요한 기본 설정 코드입니다. 셀레니움 설치된 버전 확인 pip list # 결과창에서 selenium 찾기 mac은 pip 대신 pip3 셀레니움 설치 pip install selenium #window pip3 install selenium #mac 셀레니움
goddino.tistory.com
위의 사이트를 참고하여 해결하였다.
우선
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_experimental_option("detach", True) #브라우저 꺼짐 방지 코드
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options = chrome_options) #크롬드라이버를 최신으로 유지해줍니다.
이 코드들을 추가해주어
from requests import get
#pip install requests
from bs4 import BeautifulSoup
from extractiors.wwr import extract_wwr_jobs
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
chrome_options = Options()
chrome_options.add_experimental_option("detach", True) #브라우저 꺼짐 방지 코드
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options = chrome_options)
browser.get("https://kr.indeed.com/jobs?q=python&limit=50")
이렇게 코드를 작성하여 자동 꺼짐을 방지하였다.
프린트 결과 block당하지 않고
정보가 잘 나오는 것을 볼 수 있다.
다음 글에서 이어보겠다~!
'Python > 웹 스크래핑' 카테고리의 다른 글
[Python] 웹 스크래핑 #6 (0) | 2023.01.05 |
---|---|
[Python] 웹 스크래핑 #5 (0) | 2023.01.03 |
[Python] 웹 스크래핑 #3 (0) | 2023.01.01 |
[Python] 웹 스크래핑 #2 (0) | 2023.01.01 |
[Python] 웹 스크래핑 #1 (0) | 2022.12.31 |