因此,正如某人在此问题的上一篇文章中所建议的那样,我将一切缩进了四个空格,因为我使用的代码是由另一个用户创建的,我正在复制和粘贴以保留缩进。我还尝试按照其他人的建议执行之前将原始代码保存到py文件,但仍然出现错误,粘贴在代码底部的下面。
def races(main_url):
driver = webdriver.Chrome()
driver.get(main_url)
driver.implicitly_wait(2)
races = driver.find_elements_by_class_name('time-location')
races = [race.text[:5] for race in races]
races = [race.replace(':', '') for race in races]
driver.close()
return races
def scrape(url):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(2)
driver.find_elements_by_class_name('racecard-ajax-link')[1].click()
WebDriverWait(driver,5).until(expected_conditions.presence_of_element_located((By.XPATH, '//* [@id="tab-racecard-sectional-times"]/div/div[1]/div[1]/div[2]/div/button')))
for horse in driver.find_elements_by_class_name('card-item'):
horseName = horse.find_element_by_class_name('form-link').text
times = horse.find_elements_by_class_name('sectionals-time')
times = [time.text for time in times]
print('{}: {}'.format(horseName, times))
print()
driver.close()
def main():
date = '6-October-2018'
main_url = 'http://www.attheraces.com/racecard/Wolverhampton/' + date
for race in races(main_url):
url = main_url + '/' + race
print(url)
scrape(url)
if __name__ == '__main__':
main()
回溯(最近通话最近): 文件“ C:\ Users \ djcro \ AppData \ Local \ Programs \ Python \ Python37-32 \ testing1.py”,第40行,在 主要() 主文件“ C:\ Users \ djcro \ AppData \ Local \ Programs \ Python \ Python37-32 \ testing1.py”,第34行 用于比赛(main_url): 文件“ C:\ Users \ djcro \ AppData \ Local \ Programs \ Python \ Python37-32 \ testing1.py”,第2行,在比赛中 驱动程序= webdriver.Chrome() NameError:名称“ webdriver”未定义
答案 0 :(得分:1)
假设您使用的是硒,您好像忘了添加“从硒导入webdriver”。
答案 1 :(得分:0)
driver = webdriver.Chrome()-这是不正确的。
您需要传递如下所示的chrome驱动程序可执行文件
driver = webdriver.Chrome(“ //// chrome / driver / executable”)