從網站上抓取資料,其中“查看更多”選項卡下隱藏了大量文本。
通過selenium單擊所有此類按鈕,然后使用beautifulsoup. 但是,一些按鈕的 HTML 標簽中有額外的空格。將它們復制并粘貼到browser.find_element_by_class_name('')總是會產生錯誤。
class="pv-profile-section__see-more-inline pv-profile-section__text-truncate-toggle
artdeco-button artdeco-button--tertiary artdeco-button--muted"
請注意 artdeco 后面有額外的空白,有人可以幫我嗎?手動添加這些空格或將它們放在同一行中不會做任何事情。
uj5u.com熱心網友回復:
正如答案中提到的@HedgeHog,您將無法在其中傳遞多個類名
driver.find_element(By.CLASS_NAME, "classA classB classC classD")
因為它可能會引發無效選擇器。
此外,當您發現多個帶有額外空格的類名pv-profile-section__see-more-inline, pv-profile-section__text-truncate-toggle,artdeco-button--tertiary等時,理想的方法是考慮一個唯一的類名,例如pv-profile-section__see-more-inline,它對于給定元素的功能來說似乎是獨一無二的,即see more您可以使用以下定位器策略:
使用
class_name:browser.find_element(By.CLASS_NAME, "classname")使用
css_selector:browser.find_element(By.CSS_SELECTOR, "input.pv-profile-section__see-more-inline")使用
xpath:browser.find_element(By.XPATH, "input[@class='pv-profile-section__see-more-inline']")
uj5u.com熱心網友回復:
注意:您將無法通過多個類名作為引數傳遞,find_element_by_class_name()因為它只接受一個類名
要通過多個類名查找元素嘗試使用css-選擇器:
browser.find_element_by_css_selector(".firstClassName.secondClassName")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/381167.html
上一篇:scrapy沒有通過鏈接爬行
下一篇:rvest沒有捕獲整個表
