python查看cookie

 

用到库 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



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值