Python NameError:名称未定义问题

时间:2019-09-11 14:13:39

标签: python selenium-webdriver web-scraping webdriver

运行此代码时显示错误。我已经对在足球中抓取两个或多个日期的代码进行了一些更改。我使用了webdriver,但是当它结束时显示了错误。首先,我插入了链接,其次,我插入了日期(例如2010-2014),但是当我完成该过程时,出现此错误:

Error NameError: name 'url_list' is not defined 

代码如下:

from time import sleep

from urllib.parse import urlparse

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException


def get_urls_season(url_path):

    driver = webdriver.Chrome()
    driver.fullscreen_window()
    driver.get("https://us.soccerway.com" + url_path)
    click_privacy_policy(driver)
    date = date_selector(driver)
        #url_list = cycle_through_game_weeks(driver)
    url_list.reverse()
    driver.quit()

    print("=" * 100)
    print(f"{len(set(url_list))} find")

    if input("con? (y/n): ") != "y":
        exit()

    return url_list
def date_selector(driver):
    inptdate=''
    startdate=inptdate.split('-')[0]
    enddate=inptdate.split('-')[1]

    while int(startdate)< int(enddate):
        textstring=str(startdate) + "/" + str(int(startdate)+1)
        print(textstring)
        driver.find_element_by_xpath("//select[@name='season_id']/option[text()='" + textstring +"']").click()
        startdate=int(startdate)+1
        url_list = cycle_through_game_weeks(driver)

def click_privacy_policy(driver):
    try:
        driver.find_element_by_class_name("qc-cmp-button").click()
    except NoSuchElementException:
        pass


def cycle_through_game_weeks(driver):
    season_urls = get_fixture_urls(innerhtml_soup(driver))

    while is_previous_button_enabled(driver):
        click_previous_button(driver)
        sleep(2)

        urls = get_fixture_urls(innerhtml_soup(driver))


        urls.reverse()
        season_urls += urls

    return season_urls


def is_previous_button_enabled(driver):
    return driver.find_element_by_id(
        "page_competition_1_block_competition_matches_summary_5_previous"
    ).get_attribute("class") != "previous disabled"


def click_previous_button(driver):
    driver.find_element_by_id(
        "page_competition_1_block_competition_matches_summary_5_previous"
    ).click()


def get_fixture_urls(soup):

    urls = []
    for elem in soup.select(".info-button.button > a"):
        urls.append(urlparse(elem.get("href")).path)
    return urls


def innerhtml_soup(driver):

    html = driver.find_element_by_tag_name("html").get_attribute("innerHTML")
    soup = BeautifulSoup(html, "html.parser")
    return soup

我需要解决以下问题:NameError: name is not defined问题。

1 个答案:

答案 0 :(得分:0)

您(或某人)注释了一个代码行:

#url_list = cycle_through_game_weeks(driver)

如果将其注释掉,则不会设置变量,而是设置下一行:

url_list.reverse()

正在尝试使用该变量。

取消第一行注释如何?为什么首先将其注释掉?