텍스트 스크랩시 no such element: Unable to locate element 이런 메시지가 나옵니다
조회수 3251회
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
session = requests.Session()
url = 'https://amzn.to/35lnOFf'
res = session.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}).content
soup = BeautifulSoup(res, 'lxml')
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome('./chromedriver', options=chrome_options)
driver.get(url)
asin = driver.find_element_by_id('detailBullets_feature_div').text
print(asin)
================================================================= 위 코드를 실행시 이런에러가 나옵니다.이 id아래의 모든 text를 수집하고자 합니다. 방법을 부탁드립니다.고맙습니다 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"detailBullets_feature_div"} (Session info: headless chrome=78.0.3904.108) (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64)
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
from requests_html import HTMLSession session = HTMLSession() url = 'https://amzn.to/35lnOFf' res = session.get(url) res.html.render() # Javascript Render targets = [x.text for x in res.html.find("#detailBullets_feature_div li")] for target in targets: print(target)
Javascript를 Render해야 하셔서 Selenium을 사용하신 것 같지만
어차피 headless라면, requests 라이브러리 개발자가 만든 requests-html을 사용하시면 렌더링과 Element Select가 굉장히 편리합니다.requests-html 설치
pip3 install requests-html
댓글 입력