Python练习-Instagram Explorer

新手爬虫练习之 Instagram Explore - 利用selenium获取cookies

Instagram的Explore界面内容是与账号绑定的,我们需要获取账号对应的Cookie附加在请求头上来获取需要的信息。

// Get cookies

import json
import os
import click
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def get_cookies():

	username = input("please enter username: ")
	password = input("please enter password: ")
	# choose a path to save cookies
	folder_path = 'C:\Users\Desktop\Python\savedcookies'
    file_path = 'C:\Users\Desktop\Python\savedcookies\%s.txt' % username.split('@')[0]
    cookie_dict={}
    
    if not os.path.isdir(folder):    # Create file folder if not exist
        click.echo("Create new folder to save cookies")
        time.sleep(1)
        os.mkdir(folder) 
    
    if os.path.isfile(file):        # If user in record, use saved cookies
        click.echo("Read cookies from file")
        f = open(file, 'r')        
        js = f.read()               # Read cookies
        cookie_dict = json.loads(js)
        f.close()
        click.echo("Complete...")    
    else: 							# Get cookies if not exists
        try:                        # Use selenium to get cookies
            option = webdriver.ChromeOptions()
            option.add_argument('headless') # Set Chrome Work in background
            driver = webdriver.Chrome(chrome_options=option) 
            driver.get('https://www.instagram.com/accounts/login/?next=/explore/')
            driver.find_element_by_name('username').send_keys(username)
            driver.find_element_by_name('password').send_keys(password, Keys.ENTER)
            time.sleep(5)
            driver.get(BASE_URL)
            time.sleep(5)
            cookies = driver.get_cookies()
        finally:
            driver.close()
    	# save cookies in file
        for cookie in cookies:
            cookie_dict[cookie['name']]=cookie['value']      
        click.echo("Complete...")
        click.echo("Save cookies...")
        js = json.dumps(cookie_dict)  
        f = open(file, 'a') 
        f.write(js)  
        f.close()  
    return cookie_dict

代码部分结束,均为自己编写,新手入门,欢迎建议。

你可能感兴趣的:(爬虫)