所以我試圖進入網路抓取和網路自動化。我剛開始使用硒,所以我有點不知道該怎么做。我在這里尋找過類似的問題,但到目前為止都沒有作業,所以問題可能是什么?
這是我的代碼:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = 'https://starcitygames.com/search/?search_query=Lantern of insight'
browser = webdriver.Chrome()
browser.get(url)
browser.maximize_window()
browser.implicitly_wait(20)
prices = browser.find_element_by_css_selector('div.hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes')
browser.switch_to.frame(prices)
for each in prices:
p = each.text
print(p)
我收到以下錯誤訊息:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes"}
如果它有助于我嘗試訪問的元素的 html 如下:
<div class="hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes">.99</div>
那么我能改變什么?我已經從 find_element_by_class 切換到 css 選擇器。
提前感謝您的幫助!
uj5u.com熱心網友回復:
您的方法存在一些問題。
首先,不推薦使用 find_element_by_* 函式,因此您應該改用 find_element() 函式。你需要這個匯入來做到這一點。
from selenium.webdriver.common.by import By
考慮到您正在嘗試遍歷找到的元素,我想您打算使用 find_elements() 它回傳與給定定位器匹配的元素串列。
使用 cssSelector 時,需要指定 html 標簽然后打開 [] 括號并指定屬性。
在你的情況下,這應該作業
prices = browser.find_elements(By.CSS_SELECTOR, "div[class='hawk-results-item__options-table-cell hawk-results-item__options-table-cell--price childAttributes']")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482286.html
上一篇:如何僅從9gag抓取圖片帖子
