phantomjs

[Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)

给你一囗甜甜゛ 提交于 2020-01-09 09:29:34
最近在使用Python爬取网页内容时,总是遇到JS临时加载、动态获取网页信息的困难。例如爬取CSDN下载资源评论、搜狐图片中的“原图”等,此时尝试学习Phantomjs和CasperJS来解决这个问题。这第一篇文章当然就是安装过程及入门介绍。 一. 安装Phantomjs 下载地址: http://phantomjs.org/ 官网介绍: PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. Full web stack No browser required. PhantomJS是一个服务器端的 javascript API 的 WebKit (开源的浏览器引擎)。其支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas 和 SVG。PhantomJS可以用于页面自动化,网络监测,网页截屏,以及无界面 测试 等。 下载PhantomJS解压后如下图所示: 在该文件夹下创建test.js文件,代码如下: [javascript] view plain copy

[转]关于反爬虫的总结

喜你入骨 提交于 2020-01-09 00:38:23
1、爬取过程中的302重定向 在爬取某个网站速度过快或者发出的请求过多的时候,网站会向你所在的客户端发送一个链接,需要你去验证图片。我在爬链家和拉钩网的过程中就曾经遇到过: 对于302重定向的问题,是由于抓取速度过快引起网络流量异常,服务器识别出是机器发送的请求,于是将请求返回链接定到某一特定链接,大多是验证图片或空链接。 在这种时候,既然已经被识别出来了,就使用代理ip再继续抓取。 2、headers头文件 有些网站对爬虫反感,对爬虫请求一律拒绝,这时候我们需要伪装成浏览器,通过修改http中的headers来实现   1 headers = { 2 'Host': "bj.lianjia.com", 3 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 4 'Accept-Encoding': "gzip, deflate, sdch", 5 'Accept-Language': "zh-CN,zh;q=0.8", 6 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari

python爬虫数据抓取方法汇总

和自甴很熟 提交于 2020-01-08 21:56:00
概要:利用python进行web数据抓取方法和实现。 1、python进行网页数据抓取有两种方式: 一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返回的内容。 一、第一种方法通常用来获取静态页面内容,比如豆瓣电影内容分类下 动画 对应的链接: http://www.douban.com/tag/%E5%8A%A8%E7%94%BB/?focus=movie 纪录片 对应的链接: http://www.douban.com/tag/%E7%BA%AA%E5%BD%95%E7%89%87/?focus=movie       tag 与 /?foucus中间的代表关键字,每次将页面对应的关键字进行替换就能抓取到相应的页面。           二、第二种方法是通过使用post请求来进行获取web内容抓取,由于许多网站是动态网站,每次请求返回的对应链接都是无变化,所以不能直接使用get方法来抓取网站内容,基本思路是只能依据每次发送的post数据请求观察其中的参数,并模拟构造post请求实现相应的页面获取。 2、python简易代码实现web抓取: 1 #coding=utf-8 2 3 import urllib,urllib2 4 5 #继续以抓取豆瓣电影分类链接为例 6 7 movie_list = ['%E7%BA%AA%E5

常见的反爬虫和应对方法

瘦欲@ 提交于 2020-01-08 13:46:11
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 从功能上来讲,爬虫一般分为数据采集,处理,储存三个部分。这里我们只讨论数据采集部分。 一般网站从三个方面反爬虫:用户请求的Headers,用户行为,网站目录和数据加载方式。前两种比较容易遇到,大多数网站都从这些角度来反爬虫。第三种一些应用ajax的网站会采用,这样增大了爬取的难度。 通过Headers反爬虫 从用户请求的Headers反爬虫是最常见的反爬虫策略。很多网站都会对Headers的User-Agent进行检测,还有一部分网站会对Referer进行检测(一些资源网站的防盗链就是检测Referer)。如果遇到了这类反爬虫机制,可以直接在爬虫中添加Headers,将浏览器的User-Agent复制到爬虫的Headers中;或者将Referer值修改为目标网站域名。对于检测Headers的反爬虫,在爬虫中修改或者添加Headers就能很好的绕过。 解决办法: 收集整理常见的UserAgent以供使用 ua_list = ["Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_2 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18

selenium+phantomjs解析JS

大城市里の小女人 提交于 2020-01-08 10:22:09
背景知识: PhantomJS 是一个基于WebKit的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。PhantomJS可以用于页面自动化,网络监测,网页截屏,以及无界面测试等。 Selenium也是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7、8、9)、Mozilla Firefox、Mozilla Suite等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。 PhantomJS 用来渲染解析JS,Selenium 用来驱动以及与 Pyt #coding=utf-8 from selenium import webdriver driver = webdriver.PhantomJS(executable_path=‘C:UsersGentlyguitarDesktopphantomjs-1.9.7-windowsphantomjs.exe‘) driver.get("http://phperz.com/") driver.find_element_by_id(‘search_form_input

Python/Selenium/PhantomJS - Data retained between execution

独自空忆成欢 提交于 2020-01-07 03:01:25
问题 I am trying to learn Selenium. I am using Python 2.7. Phantom JS - 2.1.1. Background - The script is trying to enter data into the controls. The script is able to catch hold of controls. However the data from older execution is retained. SCREENSHOT ADDITIONAL DETAILS As you can see in the EMAIL box, the last execution data is retained. In Checkboxes I clicked the same checkbox and then it appears unselected. As for Name field - I used clear() method and the earlier data was cleared. Same

Python/Selenium/PhantomJS - Data retained between execution

有些话、适合烂在心里 提交于 2020-01-07 03:01:03
问题 I am trying to learn Selenium. I am using Python 2.7. Phantom JS - 2.1.1. Background - The script is trying to enter data into the controls. The script is able to catch hold of controls. However the data from older execution is retained. SCREENSHOT ADDITIONAL DETAILS As you can see in the EMAIL box, the last execution data is retained. In Checkboxes I clicked the same checkbox and then it appears unselected. As for Name field - I used clear() method and the earlier data was cleared. Same

Python Webdriver raises http.client.BadStatusLine error

六眼飞鱼酱① 提交于 2020-01-07 02:16:12
问题 I'm writing a parser and I'm using Selenium Webdriver. So, I have this https://repl.it/Dgtp code and it's working fine until one random element and throws following exception: http.client.BadStatusLine: '' Don't know how to fix it at all. Help. [UPD] I tried to scroll the page via webdriver (it had to cause thumbnails to load) and got this https://repl.it/DkiX error series. It would be caused by HTTP error from one of images which were loading, but I've not found any loading errors on the

PhantomJS onResourceReceived no stream

我的未来我决定 提交于 2020-01-06 19:42:35
问题 I am trying to receive the URL of a mpeg live stream. Because the url of streams might change, but a site rarely does, I am using onResourceReceived to listen for the url of the stream. So far, I have not been getting anything (not even errors). Here is my code: var page = require('webpage').create(), system = require('system'), address; if (system.args.length === 1) { console.log('Usage: netlog.js <some URL>'); phantom.exit(1); } else { address = system.args[1]; page.onResourceRequested =

Can not evaluate jquery function in phantomjs

南笙酒味 提交于 2020-01-06 19:36:51
问题 I'd like to build a phantomjs broker service which embed with jquery and accept url and javascript code. I write a example work well: var webPage = require('webpage'); var page = webPage.create(); page.onInitialized = function() { page.injectJs('js_lib/jquery.min.js'); // page.includeJs('http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js'); console.log('running document-start script.'); }; page.open("http://example.com", function(status) { if ( status === "success" ) { // var title = page