0

skillshare-downloader says:

grab your cookie by typing:

document.cookie

Then it says:

Copy-paste cookie from developer console (without " if present) into example script.

Example:

from downloader import Downloader

cookie = """
ADD YOUR COOKIE HERE
"""

It adds an extra step.

Is there any way we can save document.cookie output to a file so that we can just read the cookie from the file instead of going to the console and type document.cookie and copy-paste the output?

I checked How to write console.log to a file instead. I also checked Python open browser and run javascript function. It suggests using Selenium or webbroser module. However, I am not sure how to approach this problem.

What can I do?

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87

1 Answers1

1

Assuming that you are using chrome:

  1. Install selenium by running in a terminal pip install selenium
  2. Install a chromedriver via a manager (It allows you to control chrome) by running in a terminal pip install webdriver-manager
  3. Create a file example.py and paste this inside

   

#import the selenium webdriver and the chromedriver
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep

#trying to stop skillshare from detecting we a are a bot
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')

#create the instance of chrome
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

#get command used to open an url
driver.get('https://www.skillshare.com/')


#login part
#press on sign in
driver.find_element_by_css_selector("a.button.alt-white-ghost.transparent.initialized").click()

#change email here
driver.find_element_by_name("email").send_keys("testmail@mail.com")

#change password here
driver.find_element_by_name("password").send_keys("fakepassword")

#wait a second
sleep(1)

#click on sign in
driver.find_element_by_xpath("//span[text()='Sign In']/parent::button").click()

#wait 3 seconds for the login
sleep(3)

#execute_script is used to execute the command in the browser console, using return here to store it in a variable
cookie = driver.execute_script('return document.cookie')

#python way of creating a file on the given path and write the cookie inside it
f = open("D:\cookie.txt", "w")
f.write(cookie)
f.close()

#closing the chrome instance
driver.close()

open a terminal and type python example.py and it will run the script

art_architect
  • 919
  • 6
  • 8
  • Do I have to login to https://www.skillshare.com/ each time or do we have a way to maintain the login state when using selenium? – Ahmad Ismail May 03 '21 at 13:31
  • 1
    hey, im not sure how long that cookie lasts after the login on that site. You have 2 options: 1. I can add the login steps and generate the cookie after or 2. launch the chrome with your user profile (same as a normal chrome, where you are already logged in). Both are easy to do. – art_architect May 03 '21 at 23:14
  • thank you very much. I actually change few lines to use it in Linux with Firefox. I will be glad if you kindly add the login steps and generate the cookie after. – Ahmad Ismail May 04 '21 at 08:58
  • 1
    I've added the login steps, unfortunately the site has a CAPTCHA. hopefully that part with options will hide it from the site. – art_architect May 04 '21 at 14:45
  • there is a [question](https://stackoverflow.com/questions/67517044/load-firefox-user-profile-with-particular-settings-in-user-agent-switcher-and-sp) related to this. Will be glad if you can kindly help. – Ahmad Ismail May 13 '21 at 09:41