用到库 selenium + phantomjs
Cookie 是由 Internet 站点创建的、将信息存储在计算机上的文件,例如访问站点时的首选项。例如,如果你在taobao的站点上查阅了航班时刻表,该站点可能就创建了包含你的旅行计划的 Cookie。也可能只记录了你在该站点上曾经访问过的页面,由此帮助你下次访问该站点时自定义查看。
下面的内容写了如何在登录账号的时候获取某个网站发来的cookie。
首先,请先安装selenium
pip install selenium
然后去http://phantomjs.org/download.html安装phantomjs:
PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. ——http://phantomjs.org/
以下是源码
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
def cookieFromTaobao():
url = 'www.XXX.com'
gets = dict(DesiredCapabilities.PHANTOMJS)
gets["phantomjs.page.settings.userAgent"] = (
" Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")
#这加入的user agent是模拟浏览器访问网站
driver = webdriver.PhantomJS(executable_path='your/path/of/phantomjs',#这里是刚才链接下载的phantomjs的phantomjs-2.1.1-macosx/bin/phantomjs文件
desired_capabilities=gets)
driver.get(url)
driver.set_window_size(1920, 1080)#加了这句就不会出现"not interactable"的问题了
driver.find_element_by_class_name('login__btn').is_enabled()
driver.find_element_by_class_name('login__btn').click()
driver.find_element_by_name("username").clear()
driver.find_element_by_name("password").clear()
driver.find_element_by_name('username').send_keys('your_user_name')
driver.find_element_by_name('password').send_keys('password')
driver.find_element_by_class_name('login__btn').click()
# 获得 cookie信息
cookies = driver.get_cookies()
print (cookies)
cookie_dict = {}
for cookie in cookies:
# 写入文件
with open('cookie.txt', 'a') as f:
f.write('\n')
for key, value in cookie.items():
with open('cookies.txt', 'a') as f:
f.write('{}:'.format(key))
f.write('{}\n'.format(value))
with open('cookies.txt', 'a') as f:
f.write('\n\n\n//')
return cookie_dict
修改一下网站和用户名密码就可以了,保存的就是cookie内容,其中有过期时间,发起者,域名等信息
完
参:
http://phantomjs.org/
https://stackoverflow.com/questions/37903536/phantomjs-with-selenium-error-message-phantomjs-executable-needs-to-be-in-pa