0

I want to call these functions from a separate Python file.

I want to specify the URL in run.py and run the functions from download.py in run.py...

But I keep getting this error below

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    getTumblrPage()
  File "/Users/matt/Python/Instagram Bots/testing/download.py", line 20, in getTumblrPage
    driver.get(url)
NameError: name 'driver' is not defined

I've tried putting the variables in both files, or just one. Either way, not sure what to do.

Below is download.py

from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep
import urllib.request
import pandas as pd
import requests
import urllib
import base64
import csv
import time

def getTumblrPage():
    driver.get(url)

# If page is infinity scroll, use this method to scrape more photos
def pageScroll():
    driver.maximize_window()
    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    print ('Page scroll 1/5')
    sleep(2)
    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    print ('Page scroll 2/5')
    sleep(2)
    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    print ('Page scroll 3/5')
    sleep(2)
    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    print ('Page scroll 4/5')
    sleep(2)
    driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
    print ('Page scroll 5/5')
    sleep(2)

Below is run.py

from download import *

url = "https://vjeranski.tumblr.com/"
driver = webdriver.Chrome(executable_path="/Users/matt/Python/chromedriver")

getTumblrPage()
pageScroll()
2
  • Python can call the function just fine. The function has an error, though, in that it tries to use a global variable, driver, that isn't defined. Python global variables aren't global across the entire process, they are module global. Commented Sep 24, 2020 at 0:26
  • 1
    I suggest you take some time to learn about variable scope and modules in Python. This will help you avoid these mistakes in the future. Commented Sep 24, 2020 at 0:27

1 Answer 1

2

If you're importing download into run, download doesn't have access to variables in run.

Instead, make driver a parameter of the functions:

def pageScroll(driver):
    . . .

def getTumblrPage(driver):
    . . .

Then pass it in when you call the functions:

driver = webdriver.Chrome(executable_path="/Users/matt/Python/chromedriver")

getTumblrPage(driver)
pageScroll(driver)
Sign up to request clarification or add additional context in comments.

3 Comments

Traceback (most recent call last): File "app.py", line 8, in <module> getTumblrPage(driver) TypeError: getTumblrPage() takes 0 positional arguments but 1 was given
@mattwelter you didn't pay attention to how your function is supposed to be defined
@mattwelter Yes, note the first block of code I posted. You need to give the functions each a parameter (the name within ()s). If you aren't familiar with parameters, I would look into them unless you're currently in the middle of studying something else. They are fairly fundamental to programming, and you will need to have a good understanding of them to write Python (or really any language).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.