[英]Python: List Comprehension Destroys Selenium While Loop
我已經嘗試搜索 inte.net 的內容來尋找這個特定難題的答案; 但是,我並沒有太幸運地深入了解這種特定情況。
因此,我目前正在嘗試為“Jazz Metal”(參見 URL)抓取 last.fm 條目的最后四頁左右。
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.headless = True
driver = webdriver.Firefox(options = options)
driver.get('https://www.last.fm/tag/jazz+metal/artists?page=20')
super_list = []
wait = WebDriverWait(driver, 10)
while True:
try:
entries = wait.until(
EC.presence_of_element_located((By.CLASS_NAME, 'grid-items-section'))
)
grid = driver.find_element(By.CLASS_NAME, 'grid-items-section')
grid_children = grid.find_elements(By.TAG_NAME, 'li')
super_list.append(grid_children)
pagination = wait.until(
EC.presence_of_element_located((By.CLASS_NAME, 'pagination-next'))
)
pagination.click()
except:
break
問題是, super_list.append(grid_children)
不是很有用,因為一旦 while 循環結束並且我在super_list
之外使用 super_list,我就不能再調用.text
方法來獲取內容,只剩下一個人類幾乎無法理解的列表。
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="11b49c8e-eec7-45f2->9e2a-e2034b93077a", element="ffe29b8e-5b65-4df3-985e-68e501e3a546")>
但是,如果我將super_list.append(grid_children)
更改為super_list.append([entry.text for entry in grid_children])
,整個 cookie 就會崩潰。 是什么賦予了? 此外,如果我完全刪除super_list.append(grid_children)
,那么它會訪問每一頁(是的,就目前而言,它甚至不會訪問最后一頁)!
plot變粗了,好像我包括了
finally:
driver.quit()
那么只有第一頁被訪問。 有人可以幫我解決這個黑魔法嗎?
好吧,我已經正式放棄 Selenium。我要 go 回到requests-html
。 很抱歉讓任何來這里尋找解決方案的人失望。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.