在本文中,我们将给您介绍关于PhantomJS的承诺框架?的详细内容,并且为您解答phantomjs配置的相关问题,此外,我们还将为您提供关于16、web爬虫讲解2—PhantomJS虚拟浏览器+se
在本文中,我们将给您介绍关于PhantomJS的承诺框架?的详细内容,并且为您解答phantomjs配置的相关问题,此外,我们还将为您提供关于16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS、CentOS NodeJS PhantomJS PhearJS 失败的尝试……、java.lang.IllegalStateException:该驱动程序不可执行:/resources/phantomjs-2.1.1-linux-x86_64/bin/phantomjs、javascript – PhantomJS / CasperJS网站登录,Cookies不接受PhantomJS的知识。
本文目录一览:- PhantomJS的承诺框架?(phantomjs配置)
- 16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS
- CentOS NodeJS PhantomJS PhearJS 失败的尝试……
- java.lang.IllegalStateException:该驱动程序不可执行:/resources/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
- javascript – PhantomJS / CasperJS网站登录,Cookies不接受PhantomJS
PhantomJS的承诺框架?(phantomjs配置)
这就是我所得到的:
var page = require('webpage').create(); page.open('http://example.com/secretpage',function(status) { console.log(status); if(status !== 'success') { console.log('Unable to access network'); } else { var links = page.evaluate(function() { var nodes = []; var matches = document.querySelectorAll('.profile > a'); for(var i = 0; i < matches.length; ++i) { nodes.push(matches[i].href); } return nodes; }); links.forEach(function(link) { console.log(link); page.open(link,function(status) { // <---- tries opening every page at once console.log(status); var name = page.evaluate(function() { return document.getElementById('username').innerHTML; }); console.log(name); page.render('profiles/'+name + '.png'); }); }); } // phantom.exit(); });
有没有办法按顺序打开每个链接?
解决方法
这是一个非常基本的实现
phantom.injectJs('async.js'); var q = async.queue(function (task,callback) { page.open(task.url,function(status) { // <---- tries opening every page at once if(status !== 'success') { console.log('Unable to open url > '+task.url); } else { console.log('opened '+task.url); //do whatever you want here ... page.render(Date.Now() + '.png'); } callback(); }); },1); // assign a callback q.drain = function() { console.log('all urls have been processed'); phantom.exit(); } var page = require('webpage').create(); page.open('http://phantomjs.org/',function(status) { console.log(status); if(status !== 'success') { console.log('Unable to access network'); } else { var links = page.evaluate(function() { var nodes = []; var matches = document.querySelectorAll('a'); for(var i = 0; i < matches.length; ++i) { nodes.push(matches[i].href); } return nodes; }); links.forEach(function(link) { q.push({url: link},function (err) { console.log('finished processing '+link); }); }); } });
URL被添加到队列中并且将被并行处理(达到并发限制,一个在这里).我重复使用相同的页面实例,但这不是强制性的.
像过去我已经做过这样的爬虫了,我再给你两个建议:
>不要加载图像以加快测试速度> href有时是相对的,所以先检查一下它是否是一个有效的url
16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS
【百度云搜索,搜各种资料:http://www.bdyss.cn】
【搜网盘,搜各种资料:http://www.swpan.cn】
PhantomJS虚拟浏览器
phantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,利用这个软件,可以获取到网址js加载的任何信息,也就是可以获取浏览器异步加载的信息
下载网址:http://phantomjs.org/download... 下载对应系统版本
下载后解压PhantomJS文件,将解压文件夹,剪切到python安装文件夹
然后将PhantomJS文件夹里的bin文件夹添加系统环境变量
cdm 输入命令:PhantomJS 出现以下信息说明安装成功
selenium模块是一个python操作PhantomJS软件的一个模块
selenium模块PhantomJS软件
webdriver.PhantomJS()实例化PhantomJS浏览器对象
get(''url'')访问网站
find_element_by_xpath(''xpath表达式'')通过xpath表达式找对应元素
clear()清空输入框里的内容
send_keys(''内容'')将内容写入输入框
click()点击事件
get_screenshot_as_file(''截图保存路径名称'')将网页截图,保存到此目录
page_source获取网页htnl源码
quit()关闭PhantomJS浏览器
#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver #导入selenium模块来操作PhantomJS
import os
import time
import re
llqdx = webdriver.PhantomJS() #实例化PhantomJS浏览器对象
llqdx.get("https://www.baidu.com/") #访问网址
# time.sleep(3) #等待3秒
# llqdx.get_screenshot_as_file(''H:/py/17/img/123.jpg'') #将网页截图保存到此目录
#模拟用户操作
llqdx.find_element_by_xpath(''//*[@id="kw"]'').clear() #通过xpath表达式找到输入框,clear()清空输入框里的内容
llqdx.find_element_by_xpath(''//*[@id="kw"]'').send_keys(''叫卖录音网'') #通过xpath表达式找到输入框,send_keys()将内容写入输入框
llqdx.find_element_by_xpath(''//*[@id="su"]'').click() #通过xpath表达式找到搜索按钮,click()点击事件
time.sleep(3) #等待3秒
llqdx.get_screenshot_as_file(''H:/py/17/img/123.jpg'') #将网页截图,保存到此目录
neir = llqdx.page_source #获取网页内容
print(neir)
llqdx.quit() #关闭浏览器
pat = "<title>(.*?)</title>"
title = re.compile(pat).findall(neir) #正则匹配网页标题
print(title)
PhantomJS浏览器伪装,和滚动滚动条加载数据
有些网站是动态加载数据的,需要滚动条滚动加载数据
实现代码
DesiredCapabilities 伪装浏览器对象
execute_script()执行js代码
current_url获取当前的url
#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver #导入selenium模块来操作PhantomJS
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities #导入浏览器伪装模块
import os
import time
import re
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap[''phantomjs.page.settings.userAgent''] = (''Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'')
print(dcap)
llqdx = webdriver.PhantomJS(desired_capabilities=dcap) #实例化PhantomJS浏览器对象
llqdx.get("https://www.jd.com/") #访问网址
#模拟用户操作
for j in range(20):
js3 = ''window.scrollTo(''+str(j*1280)+'',''+str((j+1)*1280)+'')''
llqdx.execute_script(js3) #执行js语言滚动滚动条
time.sleep(1)
llqdx.get_screenshot_as_file(''H:/py/17/img/123.jpg'') #将网页截图,保存到此目录
url = llqdx.current_url
print(url)
neir = llqdx.page_source #获取网页内容
print(neir)
llqdx.quit() #关闭浏览器
pat = "<title>(.*?)</title>"
title = re.compile(pat).findall(neir) #正则匹配网页标题
print(title)
【转载自:http://www.lqkweb.com】
CentOS NodeJS PhantomJS PhearJS 失败的尝试……
Install GCC 4.8+
PhearJS 需要 C11 的支持,而 CentOS 6 目前最新的 gcc 不支持。所以需要自己编译安装 gcc 。这里以 gcc 4.9.4 为例,下载链接以中科大镜像为例:
$ cd ~ $ wget -c http://mirrors.ustc.edu.cn/gnu/gcc/gcc-4.9.4/gcc-4.9.4.tar.bz2 $ tar -jxvf gcc-4.9.4.tar.bz2 $ ./contrib/download_prerequisites $ cd .. $ mkdir gcc4.9.4-build $ cd gcc4.9.4-build $ ../gcc-4.9.4/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib $ make -j4 $ sudo make install
安装完成后,可以查看 gcc 的版本:
$ gcc --version gcc (GCC) 4.9.4 copyright (C) 2015 Free Software Foundation,Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or fitness FOR A PARTIculaR PURPOSE.
Install Binutils
在 configure glibc 2.24 的过程中,会提示 as ld 版本过低,所以要首先升级 binutils 。
$ cd ~ $ wget -c http://mirrors.ustc.edu.cn/gnu/binutils/binutils-2.27.tar.gz $ tar zxvf binutils-2.27.tar.gz $ mkdir binutils2.27-build && cd binutils2.27-build $ ../binutils-2.27/configure $ make -j4 $ make install
Install GLIBC
$ wget -c http://mirrors.ustc.edu.cn/gnu/glibc/glibc-2.24.tar.gz $ tar zxvf glibc-2.24.tar.gz $ mkdir glibc2.24-build && cd glibc2.24-build $ ../glibc-2.24/configure $
configure 的过程提示:
configure: error: *** These critical programs are missing or too old: as ld *** Check the INSTALL file for required versions.
似乎要先把 binutils 也更新了……
然而,在更新了 binutils 之后,又遇到了 linux kernel header files missing or too old! 的问题……感觉还是换服务器比较合适……之前在 Archlinux 上运行 PhearJS 成功过。
checking installed Linux kernel header files... missing or too old! configure: error: GNU libc requires kernel header files from Linux 3.2.0 or later to be installed before configuring. The kernel header files are found usually in /usr/include/asm and /usr/include/linux; make sure these directories use files from Linux 3.2.0 or later. This check uses <linux/version.h>,so make sure that file was built correctly when installing the kernel header files. To use kernel headers not from /usr/include/linux,use the configure option --with-headers.
Install NodeJS
通过源码编译安装的方法:
$ wget -c https://nodejs.org/dist/v6.9.1/node-v6.9.1.tar.gz $ tar zxvf node-v6.9.1.tar.gz $ cd node-v6.9.1 $ ./configure $ make -j4 $ sudo make install
Install PhantomJS
PhantomJS 的安装比较简单,直接从 PhantomJS 官网下载 binary package ,解压,放入路径中就可以了。本文以 PhantomJS 2.1.1 为例
$ sudo yum install fontconfig $ cd ~ $ wget -c https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 $ tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 $ mv phantomjs-2.1.1-linux-x86_64 phantomjs
向 ~/.bashrc 中添加如下的内容:
export PATH=$HOME/phantomjs/bin:$PATH
Install memcached
$ sudo yum install memcached
Install PhearJS
$ cd ~ $ git clone https://github.com/Tomtomgo/phearjs.git $ cd ~/phearjs $ npm install
如果觉得 npm 太慢,国内的玩家可以考虑阿里巴巴的淘宝 NPM 镜像: https://npm.taobao.org/
先用以下命令安装 cnpm :
$ sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
然后用 cnpm install
替换 npm install
。
java.lang.IllegalStateException:该驱动程序不可执行:/resources/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
我正在尝试在Jenkins(Unix)中以无头模式运行Selenium UI测试。我在Unix环境中使用了正确的phantomJS版本。
phantomjs-2.1.1-linux-x86_64/bin/phantomjs
我收到上述错误。有见识吗?如果需要,我将提供更多详细信息。
java.lang.IllegalStateException: The driver is not executable: /resources/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
答案1
小编典典实际文件需要可执行文件才能运行。更改文件权限以使其可执行,例如:
chmod 755 /resources/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
然后重新运行。高温超导
javascript – PhantomJS / CasperJS网站登录,Cookies不接受PhantomJS
我想登录的网站是https://de.buyvip.com/.但是,我需要点击亚马逊的按钮,以便我可以用我的亚马逊帐户登录.其他登录表单不起作用. (这导致这个漫长的url,我只是从我的浏览器复制)
有人能帮我吗?
这是脚本:
var casper = require("casper").create() var fs = require('fs'); var page = "https://www.amazon.de/ap/signin?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&pageId=quarterdeckde&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&clientContext=280-1158662-4507036&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&marketPlaceId=A38GABX06X24K&openid.assoc_handle=quarterdeckde&openid.return_to=https%3A%2F%2Fde.buyvip.com%2Fsignin&openid.pape.max_auth_age=0&siteState=http%3A%2F%2Fde.buyvip.com%2Fhomepage%3Fhash%3DM"; phantom.cookiesEnabled = true; casper.start(page,function() { console.log("started"); this.fill('form#ap_signin_form',{ 'email' : 'myMail','password' : 'myPass' },true); }); casper.then(function() { fs.write("test.html",this.getHTML(),"w"); }); casper.run();
解决方法
casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
在我的情况下,我的情况是:“Mozilla / 5.0(Macintosh; Intel Mac OS X)AppleWebKit / 534.34(KHTML,像Gecko)CasperJS / 1.0.2 Phantomjs / 1.7. 0 Safari / 534.34“
今天关于PhantomJS的承诺框架?和phantomjs配置的分享就到这里,希望大家有所收获,若想了解更多关于16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS、CentOS NodeJS PhantomJS PhearJS 失败的尝试……、java.lang.IllegalStateException:该驱动程序不可执行:/resources/phantomjs-2.1.1-linux-x86_64/bin/phantomjs、javascript – PhantomJS / CasperJS网站登录,Cookies不接受PhantomJS等相关知识,可以在本站进行查询。
本文标签: