我正在嘗試從以下網頁獲取書籍描述:https ://bookshop.org/books/lucky-9798200961177/9781668002452
這是我到目前為止所擁有的
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome('path_to_my_driver_on_local', options=options)
driver.get('https://bookshop.org/a/16709/9781668002452')
description = driver.find_elements_by_xpath('//meta[@content]')[0].text
description
基本上,我正在嘗試獲取此 html 中的文本:
<meta name="description" content="REESE'S BOOK CLUB PICK NEW YORK TIMES BESTSELLER A thrilling roller-coaster ride about a heist gone terribly wrong, with a plucky protagonist who will win readers' hearts. What if you had the winning ticket ....">
但我無法在內容中找到文字。誰能建議如何獲取內容中的文本?
uj5u.com熱心網友回復:
elem=driver.find_element(By.XPATH,"//meta[@name='description']")
print(elem.get_attribute("content"))
您可以使用更具包容性的 xpath。然后定位內容的屬性。
進口:
from selenium.webdriver.common.by import By
uj5u.com熱心網友回復:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome('path_to_my_driver_on_local', options=options)
driver.get('https://bookshop.org/books/lucky-9798200961177/9781668002452')
html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
Description = soup.find_all('div', class_="title-description")
print(Description[0].text)
uj5u.com熱心網友回復:
您需要使用正確的 xpath 來定位元素。您對 xpath 的值//meta[@content]是回傳meta包含content屬性的第一個元素。我建議使用 xpath//meta[@name="description"]或 css 選擇器meta[name="description"]進行更精確的選擇。這完美地作業:
# imports and boilerplate
....
description_meta_element = driver.find_element_by_css_selector('meta[name="description"]')
description_meta_content = description_meta_element.get_attribute('content')
print(description_meta_content)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435999.html
上一篇:ObjectC-為什么我不能使用class_copyPropertyList函式正確獲取屬性?
下一篇:回圈瀏覽具有靜態URL的頁面
