Phantomjs

1、安装参考如下博客

https://blog.csdn.net/qq_26718271/article/details/55046198

2、使用

实例1:创建文件夹myJs,创建文件:loadPage.js

loadPage.js:

var page=require('webpage').create();
page.open('http://www.cnblogs.com/qiyeboy/',function(status){
console.log("Status:"+status);
if(status=="success"){
page.render('qiye.png');  //如果修改成“qiye.pdf”,则保存为pdf文件
}
phantom.exit();

});

进入cmd命令,切换到该文件所在目录,执行:phantomjs loadPage.js

可以将该网站截图为png文件,保存在本地

实例2:获取网站页面的内容:movie.js

movie.js:

phantom.outputEncoding="gb2312"   //特别注意这行代码,没有的话可能会出现乱码
var page=require('webpage').create();
console.log('The default user agent is'+page.settings.userAgent);
page.settings.userAgent="Mozilla/5.0  (windows NT 6.1;WOW; rv:49.0)Gecko/20100101 Firefox/49.0)";
page.open('http://movie.mtime.com/108737',function(status){
console.log("Status:"+status);
if(status!="success"){
console.log('Unable to access network');
}else{
var ua=page.evaluate(function(){
return document.getElementById('ratingRegion').textContent;
});
console.log(ua);
}
phantom.exit();

});

window Phantomjs、seleniu安装及基本使用


selenium

1、安装

window Phantomjs、seleniu安装及基本使用

2、需要对应的浏览器调用的补丁

下载地址:https://www.seleniumhq.org/download/

window Phantomjs、seleniu安装及基本使用

下载:

https://github.com/mozilla/geckodriver/releases/

window Phantomjs、seleniu安装及基本使用

下载解压文件,把解压得到的文件复制到pycharm使用的python.exe所在的目录

使用:

在pycharm创建文件,demo代码如下:

from selenium import  webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
assert  u"百度" in driver.title
elem=driver.find_element_by_name("wd")
elem.clear()
elem.send_keys(u"网络爬虫")
elem.send_keys(Keys.RETURN)
time.sleep(3)
assert u"网络爬虫." not in driver.page_source
driver.close()
问题:如果运行报错,可以在cmd执行如下命令,更新selenium

window Phantomjs、seleniu安装及基本使用


相关文章:

  • 2021-05-23
  • 2021-07-22
  • 2021-11-27
  • 2021-11-04
  • 2022-01-04
  • 2021-05-01
  • 2021-11-30
猜你喜欢
  • 2021-11-05
  • 2021-07-18
  • 2021-04-02
  • 2021-05-14
  • 2021-08-05
  • 2021-06-30
  • 2021-08-05
相关资源
相似解决方案