반응형

플그래밍/파이써언 172

[파이썬] Selenium - AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

AttributeError: 'WebDriver' object has no attribute 'find_element_by_id' 위와 같은 에러가 발생했을 경우 해결하는 방법입니다. # 변경 전 driver.find_element_by_id("draggable") # 변경 후 from selenium.webdriver.common.by import By driver.find_element(By.ID, "draggable") 위와 같이 변경하면 되는데요, from selenium.webdriver.common.by import By 해당 모듈을 반드시 추가해주셔야 합니다.

[파이썬] Selenium - WIKIMEDIA 메인페이지 이미지 다운로드 방법

이번에는 아래 메인 페이지에 있는 이미지의 src를 추출해서 직접 파일로 다운로드해볼께요 파이썬 코드 import requests from bs4 import BeautifulSoup from selenium import webdriver import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service from web..

[파이썬] Selenium - FnGuide 오늘의 급상승 검색어 추출하기

이번에는 FnGuide사이트에 게시되는 오늘의 급상승 검색어를 추출해볼께요 FnGuide 아래 키워드 클라우드 부분을 추출할 예정이에요 추출하면서 단어 크기에 따른 비중도 함께 추출해볼께요 사이트에 접속하면 공지사항 팝업이 뜨는데 이것부터 닫아줄께요 팝업 닫기 버튼을 찾아서 클릭해줍니다 button = driver.find_element(By.ID, "closeBtn2") button.click() 그 다음에는 워드클라우드를 찾아서 키워드를 추출하고 class를 활용하여 각 키워드의 비중 또한 추출합니다 cloud_div = driver.find_element(By.ID, "cloud") span_tags = cloud_div.find_elements(By.TAG_NAME, "span") for span..

[파이썬] Selenium - 네이버 카페 인기글 제목 추출하기

이번에는 네이버 카페 인기글 게시판의 제목을 추출해볼께요 네이버 카페 (비머베르크) 먼저 인기글을 클릭해야겠네요 link_text = "인기글" element = driver.find_element(By.LINK_TEXT, link_text) element.click() 게시글 요소를 찾을 수 없데요 네이버 카페 게시판은 동일 페이지에서도 iframe을 쓰나보네요 프레임을 바꿔볼께요 iframe_element = driver.find_element(By.ID, "cafe_main") driver.switch_to.frame(iframe_element) 이제는 잘 되겠죠 tbody안에 모든 tr태그를 크롤링해볼께요 tbody_element = driver.find_element(By.XPATH, "//tb..

[파이썬] Selenium - 네이버 스포츠 일정 및 결과 크롤링하기

이번에는 네이버 스포츠 사이트의 일별 야구 일정/결과 페이지를 크롤링해볼께요 네이버 스포츠 먼저 Selenium을 사용하여 해당 사이트로 접속합니다. import requests from bs4 import BeautifulSoup from selenium import webdriver import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_cond..

[파이썬] Selenium - 무한로딩 페이지 최하단까지 스크롤 하는법

이번에는 네이버 쇼핑 검색 후 결과 페이지의 최하단까지 스크롤하는 방법을 알아볼께요. 네이버 쇼핑 네이버 쇼핑 - 검색결과 페이지 위와 같이 검색결과 페이지는 한번에 모든 내용을 표시해주지 않아요 아래로 스크롤 할때마다 다음 목록을 새로 불러와 보여줍니다 파이썬 셀레니움(Selenium)을 활용해서 특정 키워드 검색 및 최하단까지 스크롤하는 방법을 알아볼께요 import requests from bs4 import BeautifulSoup from selenium import webdriver import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.common.keys im..

[파이썬] SSLError: HTTPSConnectionPool 에러 해결 방법

requests 사용 시 발생하는 SSLError입니다 해결 방법을 알아볼께요 import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) url = "url 입력" response = requests.post(url, verify=False) if response.status_code == 200: # You can access the response text or other properties here print(response.text) else: print(f"Error: {respo..

파이썬 셀프(self)에 대해서 알아봤어요

Python self에 대해서 알아볼께요 - 인스턴스 참조: self는 클래스의 인스턴스 자신을 참조하는 데 사용됩니다. 이를 통해 인스턴스 변수에 접근하거나 다른 메서드를 호출할 수 있습니다. class MyClass: def __init__(self): self.value = 10 - 메서드의 첫 번째 인자: 클래스 내의 모든 메서드는 첫 번째 인자로 self를 받습니다. 이를 통해 메서드가 현재 인스턴스에 접근할 수 있게 됩니다. class MyClass: def print_value(self): print(self.value) - 명시적인 사용: Python에서는 self를 명시적으로 사용해야 합니다. 이는 Python의 명시성(print)을 반영한 것입니다. my_instance = MyClas..

파이썬 람다(lambda)에 대해서 알아봤어요

Python에서 람다(lambda)가 무엇인지 알아보았어요 간략한 함수 정의: 람다는 이름 없는 함수, 즉 익명 함수를 정의하는 방법입니다. 이를 통해 코드를 간결하게 만들 수 있습니다.lambda arguments: expression 단일 표현식 사용: 람다는 단일 표현식만을 사용합니다. 복잡한 로직이나 여러 표현식을 가진 함수는 람다로 정의할 수 없습니다.double = lambda x: x * 2 일회성 사용 용이: 람다는 주로 일회성으로 사용되는 함수를 만드는 데 유용합니다. 특히 map(), filter(), reduce() 같은 함수와 함께 사용되는 경우가 많습니다.nums = [1, 2, 3, 4, 5] doubled_nums = list(map(lambda x: x * 2, nums)) ..

반응형