通过selenium的executeScrpit()可以执行js代码,
我用js代码获取数据后把它存到cookie中,然后通过python再获取cookie的值,但是值取不到,也不打印,但是也没有报错,代码如下,请高手指点一下,谢谢!
用python获取cookie的代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
import urllib
import http.cookiejar
import requests
from urllib import request
from http import cookiejar
def capture(url, save_fn="capture.png"):
browser = webdriver.Chrome() # Get local session of firefox
browser.set_window_size(1200, 900)
browser.get(url) # Load page
browser.execute_script("""
(function () {
var y = 0;
var step = 100;
window.scroll(0, 0);
var height = document.body.scrollHeight;
document.cookie = "scrollHeight="+height;
function f() {
if (y < document.body.scrollHeight) {
y += step;
window.scroll(0, y);
setTimeout(f, 50);
} else {
window.scroll(0, 0);
document.title += "scroll-done";
}
}
setTimeout(f, 1000);
})();
""")
for i in range(30):
if "scroll-done" in browser.title:
break
time.sleep(1)
cookie = cookiejar.CookieJar()
#利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler
handler=request.HTTPCookieProcessor(cookie)
#通过CookieHandler创建opener
opener = request.build_opener(handler)
#此处的open方法打开网页
#response = opener.open('http://www.jb51.net')
#打印cookie信息
for item in cookie:
print('Name = %s' % item.name)
print('Value = %s' % item.value)
browser.save_screenshot(save_fn)
browser.close()
if __name__ == "__main__":
capture("http://www.jb51.net")