簡體   English   中英

蟒蛇/硒。 列表未填滿 IMDb href

[英]Python/Selenium. List is not filling up with IMDb hrefs

嗨,我正在嘗試拼湊一些從這 4 個 IMDb 鏈接中刮取 href 的東西,但我的 list = [] 不會試圖填滿,即使我返回或打印而不是 append 我得到了列表。 它以前工作過,但也許我現在移動了一些東西它不起作用。

first_page = 'https://www.imdb.com/title/'+movie+'/episodes?season=1'
second_page = 'https://www.imdb.com/title/'+movie+'/episodes?season=2'
third_page = 'https://www.imdb.com/title/'+movie+'/episodes?season=3'
fourth_page = 'https://www.imdb.com/title/'+movie+'/episodes?season=4'
driver.get(first_page)
driver.execute_script("window.open('" + second_page +"');") 
driver.execute_script("window.open('" + third_page +"');") 
driver.execute_script("window.open('" + fourth_page +"');") 
time.sleep(3)

# Handles is a variable which handles the 
handles = driver.window_handles
# Loops through each tab and performs a function
for handle in handles:
    driver.switch_to.window(handle)
    # Scrapes all hrefs(including episode links) builds a list
    links = []
    elements = driver.find_elements_by_tag_name('a')
    for elem in elements:
        href = elem.get_attribute("href")
        links.append(href)

time.sleep(5)

driver.quit()

這是錯誤 NameError: name 'links' is not defined

正如羅伯特·科瓦奇所說,這就是答案。

links = []
# Handles is a variable which handles the 
handles = driver.window_handles
# Loops through each tab and performs a function
for handle in handles:
    driver.switch_to.window(handle)
    # Scrapes all hrefs(including episode links) builds a list

    elements = driver.find_elements_by_tag_name('a')
    for elem in elements:
       href = elem.get_attribute("href")
       if href is not None:
        links.append(href)

time.sleep(5)

driver.quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM