phantomjs的session cookie保留补丁

phantomjs本身是不能保留session cookie的。这样,一些模拟登陆的动作,就无法共享cookie给其他浏览器或者爬虫(诸如,保存为LWP格式等)

补丁,需要更改phantomjs的代码。

步骤如下:

1
2
3
4
5
git clone https://github.com/ariya/phantomjs.git phantomjs-build
cd phantomjs-build/
git checkout 2.1.1
git submodule init
git submodule update

修改文件:src/cookiejar.cpp 。找到 如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool CookieJar::purgeSessionCookies()
{
QList<QNetworkCookie> cookiesList = allCookies();
// If empty, there is nothing to purge
if (cookiesList.isEmpty()) {
return false;
}
// Check if any cookie has expired
// remove this function
int prePurgeCookiesCount = cookiesList.count();
for (int i = cookiesList.count() - 1; i >= 0; --i) {
if (cookiesList.at(i).isSessionCookie()) {
qDebug() << "CookieJar - Purged (session)" << cookiesList.at(i).toRawForm();
cookiesList.removeAt(i);
}
}
// Set cookies and returns "true" if at least 1 session cookie was found and removed
if (prePurgeCookiesCount != cookiesList.count()) {
setAllCookies(cookiesList);
return true;
}
setAllCookies(cookiesList);
return false;
}

屏蔽掉根据isSessionCookie() 删除cookie键对的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool CookieJar::purgeSessionCookies()
{
QList<QNetworkCookie> cookiesList = allCookies();
// If empty, there is nothing to purge
if (cookiesList.isEmpty()) {
return false;
}
// Check if any cookie has expired
// remove this function
int prePurgeCookiesCount = cookiesList.count();
//for (int i = cookiesList.count() - 1; i >= 0; --i) {
// if (cookiesList.at(i).isSessionCookie()) {
// qDebug() << "CookieJar - Purged (session)" << cookiesList.at(i).toRawForm();
// cookiesList.removeAt(i);
// }
//}
// Set cookies and returns "true" if at least 1 session cookie was found and removed
if (prePurgeCookiesCount != cookiesList.count()) {
setAllCookies(cookiesList);
return true;
}
setAllCookies(cookiesList);
return false;
}

然后,重新编译,执行即可。

1
python build.py