使用硒从网站中提取文本

时间:2020-07-10 16:29:58

标签: python python-3.x selenium selenium-webdriver beautifulsoup

试图找到一种方法,从良好阅读页面中提取书摘。不幸的是,尝试过美丽的汤/硒。

链接:https://www.goodreads.com/book/show/67896.Tao_Te_Ching?from_search = true&from_srp = true&qid = D19iQu7KWI&rank = 1

代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import requests
link='https://www.goodreads.com/book/show/67896.Tao_Te_Ching?from_search=true&from_srp=true&qid=D19iQu7KWI&rank=1'
driver.get(link)
Description=driver.find_element_by_xpath("//div[contains(text(),'TextContainer')]")
#first TextContainer contains the sumary of the book
book_page = requests.get(link)
soup = BeautifulSoup(book_page.text, "html.parser")
print(soup)
Container = soup.find('class', class_='leftContainer')
print(Container)

错误:

容器为空+

NoSuchElementException:没有这样的元素:无法找到元素: {“ method”:“ xpath”,“ selector”:“ // div [contains(text(),'TextContainer')]”“} (会话信息:chrome = 83.0.4103.116)

1 个答案:

答案 0 :(得分:0)

您可以像这样获得说明

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
...
driver.get("https://www.goodreads.com/book/show/67896.Tao_Te_Ching?from_search=true&from_srp=true&qid=D19iQu7KWI&rank=1")
description = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'div#description span[style="display:none"]'))
)
print(description.get_attribute('textContent'))

我已经利用CSS Selector 来获取包含完整说明的特定隐藏span。我还使用了explicit wait来给元素加载时间。