简体   繁体   中英

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

Hi I'm trying to piece together something that scrapes the hrefs from these 4 IMDb links but my list = [] won't seek to fill up even though when I return or print instead of append I get the list. It worked before but maybe I moved something now it won't work.

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()

This is the error NameError: name 'links' is not defined

As Robert Kovacs stated this is the answer.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM