我试图抓一个特定的网站。我希望能够:
<div class="inhoudsindicatie"><p><span class="hl0 highlightColor0">HR</span>: art. 81RO.</p></div>
特别是&#34;艺术。 81RO&#34;部分内容。
from selenium import webdriver
from bs4 import BeautifulSoup as soup
driver.get('http://uitspraken.rechtspraak.nl/inziendocument?id=ECLI:NL:HR:2014:3004&showbutton=true&keyword=HR%3a')
page=soup(driver.page_source, "html.parser")
details=soup.findAll("span",{"class":"hl0 highlightColor0"})
它返回:
AttributeError: 'str' object has no attribute 'descendants'
这对我的代码意味着什么?我阅读了关于后代的一般信息,我很确定我不明白。
(我的主要兴趣是理解问题,解决问题是次要的,但当然非常感激)
答案 0 :(得分:1)
这对我有用:
import time
from selenium import webdriver
from bs4 import BeautifulSoup as soup
driver = webdriver.Chrome("/path/to/chromedriver")
driver.get('http://uitspraken.rechtspraak.nl/inziendocument?id=ECLI:NL:HR:2014:3004&showbutton=true&keyword=HR%3a')
time.sleep(5)
page = soup(driver.page_source, "html.parser")
details = page.select_one("span.hl0.highlightColor0").find_parent().get_text()
print(details)
driver.quit()
# output: HR: art. 81RO.
但是既然你还在使用硒,为什么不坚持下去呢?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome("/path/to/chromedriver")
driver.get('http://uitspraken.rechtspraak.nl/inziendocument?id=ECLI:NL:HR:2014:3004&showbutton=true&keyword=HR%3a')
wait = WebDriverWait(driver, 10)
xpath = "//p/span[contains(@class, 'highlightColor0') and contains(@class, 'hl0')]/.."
details = wait.until(EC.visibility_of_element_located((By.XPATH, xpath)))
print(details.text)
driver.quit()
# output: HR: art. 81RO.
如果你不想要人力资源:&#39;部分你可以删除它:
details.split('HR: ')[1]
# output: art. 81RO.