I am getting NameError: name 'data' is not defined from selenium webscrape, but I have defined it in the...












0















from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

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

import pandas as pd

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


This is where I store the web scrape results to the variable data so struggling to see what is causing the error. Appreciate any help.



    data = [main]


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

return data

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


At this point the webscarper returns all of the data results within sublime, but it doesn't save to the csv file as it throws an NameError: name 'data' is not defined after the df = pd.DataFrame(data)



df = pd.DataFrame(data)
df.to_csv("jan_1")









share|improve this question


















  • 2





    This is local versus global variable, see e.g. here

    – Michael Butscher
    Nov 21 '18 at 21:54













  • Thanks but forgive me for being really dense I'm still not sure how to resolve this after reading that link. Am I to define the variable as global or insert "df = pd.DataFrame(data) df.to_csv("jan_1") inside the code somewhere? I've tried both with no success.

    – BrianC
    Nov 21 '18 at 23:53











  • From your code it isn't clear where the df = pd.DataFrame(data) is or should be placed. Depending on the context it may be placed into the for-loop in main. As scrape returns the data you could there write data = scrape(url) and after that the df = pd.DataFrame(data).

    – Michael Butscher
    Nov 22 '18 at 0:06













  • Unfortunately the data = [main] also doesn't make much sense and data isn't filled with the real data. Your code may have too many issues to solve them here.

    – Michael Butscher
    Nov 22 '18 at 0:09
















0















from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

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

import pandas as pd

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


This is where I store the web scrape results to the variable data so struggling to see what is causing the error. Appreciate any help.



    data = [main]


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

return data

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


At this point the webscarper returns all of the data results within sublime, but it doesn't save to the csv file as it throws an NameError: name 'data' is not defined after the df = pd.DataFrame(data)



df = pd.DataFrame(data)
df.to_csv("jan_1")









share|improve this question


















  • 2





    This is local versus global variable, see e.g. here

    – Michael Butscher
    Nov 21 '18 at 21:54













  • Thanks but forgive me for being really dense I'm still not sure how to resolve this after reading that link. Am I to define the variable as global or insert "df = pd.DataFrame(data) df.to_csv("jan_1") inside the code somewhere? I've tried both with no success.

    – BrianC
    Nov 21 '18 at 23:53











  • From your code it isn't clear where the df = pd.DataFrame(data) is or should be placed. Depending on the context it may be placed into the for-loop in main. As scrape returns the data you could there write data = scrape(url) and after that the df = pd.DataFrame(data).

    – Michael Butscher
    Nov 22 '18 at 0:06













  • Unfortunately the data = [main] also doesn't make much sense and data isn't filled with the real data. Your code may have too many issues to solve them here.

    – Michael Butscher
    Nov 22 '18 at 0:09














0












0








0








from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

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

import pandas as pd

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


This is where I store the web scrape results to the variable data so struggling to see what is causing the error. Appreciate any help.



    data = [main]


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

return data

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


At this point the webscarper returns all of the data results within sublime, but it doesn't save to the csv file as it throws an NameError: name 'data' is not defined after the df = pd.DataFrame(data)



df = pd.DataFrame(data)
df.to_csv("jan_1")









share|improve this question














from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

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

import pandas as pd

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


This is where I store the web scrape results to the variable data so struggling to see what is causing the error. Appreciate any help.



    data = [main]


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

return data

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


At this point the webscarper returns all of the data results within sublime, but it doesn't save to the csv file as it throws an NameError: name 'data' is not defined after the df = pd.DataFrame(data)



df = pd.DataFrame(data)
df.to_csv("jan_1")






python csv selenium-webdriver web-scraping






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 21:51









BrianCBrianC

55




55








  • 2





    This is local versus global variable, see e.g. here

    – Michael Butscher
    Nov 21 '18 at 21:54













  • Thanks but forgive me for being really dense I'm still not sure how to resolve this after reading that link. Am I to define the variable as global or insert "df = pd.DataFrame(data) df.to_csv("jan_1") inside the code somewhere? I've tried both with no success.

    – BrianC
    Nov 21 '18 at 23:53











  • From your code it isn't clear where the df = pd.DataFrame(data) is or should be placed. Depending on the context it may be placed into the for-loop in main. As scrape returns the data you could there write data = scrape(url) and after that the df = pd.DataFrame(data).

    – Michael Butscher
    Nov 22 '18 at 0:06













  • Unfortunately the data = [main] also doesn't make much sense and data isn't filled with the real data. Your code may have too many issues to solve them here.

    – Michael Butscher
    Nov 22 '18 at 0:09














  • 2





    This is local versus global variable, see e.g. here

    – Michael Butscher
    Nov 21 '18 at 21:54













  • Thanks but forgive me for being really dense I'm still not sure how to resolve this after reading that link. Am I to define the variable as global or insert "df = pd.DataFrame(data) df.to_csv("jan_1") inside the code somewhere? I've tried both with no success.

    – BrianC
    Nov 21 '18 at 23:53











  • From your code it isn't clear where the df = pd.DataFrame(data) is or should be placed. Depending on the context it may be placed into the for-loop in main. As scrape returns the data you could there write data = scrape(url) and after that the df = pd.DataFrame(data).

    – Michael Butscher
    Nov 22 '18 at 0:06













  • Unfortunately the data = [main] also doesn't make much sense and data isn't filled with the real data. Your code may have too many issues to solve them here.

    – Michael Butscher
    Nov 22 '18 at 0:09








2




2





This is local versus global variable, see e.g. here

– Michael Butscher
Nov 21 '18 at 21:54







This is local versus global variable, see e.g. here

– Michael Butscher
Nov 21 '18 at 21:54















Thanks but forgive me for being really dense I'm still not sure how to resolve this after reading that link. Am I to define the variable as global or insert "df = pd.DataFrame(data) df.to_csv("jan_1") inside the code somewhere? I've tried both with no success.

– BrianC
Nov 21 '18 at 23:53





Thanks but forgive me for being really dense I'm still not sure how to resolve this after reading that link. Am I to define the variable as global or insert "df = pd.DataFrame(data) df.to_csv("jan_1") inside the code somewhere? I've tried both with no success.

– BrianC
Nov 21 '18 at 23:53













From your code it isn't clear where the df = pd.DataFrame(data) is or should be placed. Depending on the context it may be placed into the for-loop in main. As scrape returns the data you could there write data = scrape(url) and after that the df = pd.DataFrame(data).

– Michael Butscher
Nov 22 '18 at 0:06







From your code it isn't clear where the df = pd.DataFrame(data) is or should be placed. Depending on the context it may be placed into the for-loop in main. As scrape returns the data you could there write data = scrape(url) and after that the df = pd.DataFrame(data).

– Michael Butscher
Nov 22 '18 at 0:06















Unfortunately the data = [main] also doesn't make much sense and data isn't filled with the real data. Your code may have too many issues to solve them here.

– Michael Butscher
Nov 22 '18 at 0:09





Unfortunately the data = [main] also doesn't make much sense and data isn't filled with the real data. Your code may have too many issues to solve them here.

– Michael Butscher
Nov 22 '18 at 0:09












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420962%2fi-am-getting-nameerror-name-data-is-not-defined-from-selenium-webscrape-but%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420962%2fi-am-getting-nameerror-name-data-is-not-defined-from-selenium-webscrape-but%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents