scrapy selenium 爬虫

1、虚拟机关闭后找不到系统了,查找我的文档下面的vmk文件,发现都不是正确的系统。按F9,虚拟机左侧显示出各种虚拟机,找到了使用的虚拟机,位置在d:/scrapy

2、发现可以ping通百度,但虚拟机里面的火狐浏览器无法上网,原因是公司内部使用代理服务器上网,设置代理,成功上网。

3、使用pip install安装selenium


4、在火狐浏览器上安装selenium IDE,以前的地址http://www.seleniumhq.org/download/ 路径太慢,好久没响应(并不是被墙了,只是反应慢而已)。换用新的安装地址:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/ 直接点击安装重启火狐浏览器  推荐书目《selenium初学者指南》

5、http://www.cnblogs.com/buzhizhitong/p/5697526.html  下载driver,使用webdriver

6、研究phantomjs爬虫 http://duchengjiu.iteye.com/blog/2201868

  1. from seleniumimport webdriver

  2. driver = webdriver.PhantomJS()
  3. driver.get('www.baidu.com')
  4. data = driver.find_element_by_id('cp').text
  5. print data

7、

一、安装

安装包下载地址: http://phantomjs.org/download.html ,包括 Windows ,Mac OS,Linux版本,自行选择对应 版本下载解压即可( 为方便使用,可自已为phantomjs设置环境变量 ),其中带有一个example文件夹,里面有很多已经写好的代码供使用。本文假设phantomjs已经安装好并已设置了环境变量。

二、使用

Hello, World!

新建一个包含下面两行脚本的文本文件:

console.log('Hello, world!');
phantom.exit();

将文件另存为 hello.js ,然后执行它:

phantomjs hello.js

输出结果为:Hello, world!

第一行将会在终端打印出字符串,第二行 phantom.exit 将退出运行。 
在该脚本中调用 phantom.exit 是非常重要的,否则 PhantomJS 将根本不会停止。

http://python.jobbole.com/86415/?utm_source=group.jobbole.com&utm_medium=relatedArticles

由于在官网 http://phantomjs.org/download.html 下载的安装包bz2,解压后不知道怎么安装,目前网上的信息大多是官网上介绍的,最后如下所示

操作系统:CentOS 6.7 32-bit

复制代码
# 安装依赖软件
yum -y install wget fontconfig

# 下载PhantomJS
wget -P /tmp/ https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-i686.tar.bz2

# 解压
tar xjf /tmp/phantomjs-2.1.1-linux-i686.tar.bz2 -C /usr/local/

# 重命名
mv /usr/local/phantomjs-2.1.1-linux-i686 /usr/local/phantomjs

# 建立软链接
ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/
复制代码

测试

在终端执行phantomjs命令,如果跟我下图的结果一样就表示安装成功了。

如果你还不放心的话,可以创建一个JS文件测试一下。文件内容如下:

console.log('Hello, world!');
phantom.exit();

保存为test.js,然后通过phantomjs test.js执行。下图是我执行的结果:

成功安装!



解决ImportError: cannot import name webdriver:

可以使用import selenium print selenium.__file__去看打印出的文件路径,如果不是下面类似的C:\Python27\lib\site-packages\selenium-2.31.0-py2.7.egg\selenium\__init__.pyc,需要把当前目录下的文件删除或者重命名。

报错:

for requests_or_item in iterate_spider_output(cb_res):

File "E:\scrapyproject\testproject\testproject\spiders\00wang_CrawlSpider.py", line 50, in parse_product

    driver = webdriver.Firefox()
  File "d:\python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__
    self.service.start()
  File "d:\python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 
解决方式:

please check the answer:

http://stackoverflow.com/a/37765661

http://stackoverflow.com/a/40208762

Shortly,

From selenium 3.0, you have to explicitly download Marionette geckodriver for Firefox (which is similar to ChromeDriver for Chrome) and keep it in a place where the system can identify it. (like System PATH - environmental variables in Windows) or specify using language options.

References:

  1. Download Marionette GeckoDriver
  2. https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver
  3. https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette
点击下载win7的64位,点击exe文件直接放到window目录下面

 报错: File "E:\scrapyproject\testproject\testproject\spiders\00wang_CrawlSpider.py", line 50, in parse_product
    driver = webdriver.Firefox()
  File "d:\python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 150, in __init__
    keep_alive=True)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "d:\python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

解决方式:


如果您觉得本文确实帮助了您,可以微信扫一扫,进行小额的打赏和鼓励,谢谢 ^_^


scrapy selenium 爬虫_第1张图片



你可能感兴趣的:(网络爬虫,Python)