说明
本文针对正常安装 pyspider 后无法使用 css_selector_helper 的问题进行解决,可能不适合所有情况,但大部分情况是如此的。
问题分析
- 无法使用 css_selector_helper
- 打开浏览器开发者工具,发现
网络
标签中存在ERR_TIME_OUT …… css_selector_helper.js
的问题,说明css_selector_helper.js加载超时; - 发现 css_selector_helper.js 试图从 https://IP:port/……/css_selector_helper.js加载,但是浏览器直接打开该链接无效,尝试将 https 改为 http 却能打开该 js 文件,说明pyspider 拼接js文件路径时有问题;
- 回到 pyspider 安装环境,分析了 pyspider 目录下的文件,发现该js文件的路径在
/usr/local/lib/python2.7/dist-packages/pyspider/webui/static/debug.js
;
解决问题
修复 static/css_selector_helper.js 的加载路径
# 此处以 ubuntu 系统环境为例,其他情况类似
sudo vi /usr/local/lib/python2.7/dist-packages/pyspider/webui/static/debug.js
# 将第447行的内容进行替换
# 原内容:
$(dom).find('body').append('<script src="'+location.protocol+'//'+location.host+'/static/css_selector_helper.js">');
# 替换为:
$(dom).find('body').append('<script src="http://'+location.host+'/static/css_selector_helper.js">');
# 修改文件 /usr/local/lib/python2.7/dist-packages/pyspider/webui/templates/helper.js
# 将第27行内容替换
# 原内容为 :
script.src = "//{{ host }}/static/css_selector_helper.js";
# 替换为
script.src = "http://"+location.host+"/static/css_selector_helper.js";