How to extract javascript code from github

Hi guys, I tried to find a way to extract ( and then run ) a js code from GitHub URL, so I began with this read and write python script but actually it brings all the GitHub page code including my code, can someone lead me to proper way:

my code:

url = "./test.csv"
resp = requests.get("github_url")
with open("test.csv", "w") as f:
    f.write(resp.text)

Thank you in advance!

Hi!

In Github click the link “Raw” on your right. This will take you to a page that is not html, but has your code only.

As for your code snippet, that seems fine except for variable naming. You are calling a local file a “url”.

(Tested with your code using the REPL, except used simplified address for my local file, “test.py”)

Hi Petteri,
Thank you for your reply, I already found a way better with python selenium using this code:

def execute_scripts():

    driver = webdriver.Chrome()
    driver.get(sys.argv[1])
    
    driver.find_element(By.ID, 'raw-url').click()
    time.sleep(2)
    
    get_url_script = driver.find_element(By.TAG_NAME, 'pre')
    
    js_script = get_url_script.text
    # print(js_script)

    result = driver.execute_script(js_script)
    
    return result

Hope it helps somebody with the same purpose

That is one way to solve the problem. I’m not sure what your exact use case is. E.g. do you need to handle different Github repos or is this one off piece of code?

If you are using a same piece of code, then you might as well provide the raw URL directly to the program. Then your execute function would be less verbose. E.g.

def execute_scripts(raw, local_file):
    r = requests.get(raw)
    with open(local_file, "w") as f:
        f.write(r.text)

    CMD = f"python {local_file}"
    os.system(CMD)