最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】尝试用Python操作PhantomJS+Selenium去模拟购物操作

Python crifan 3761浏览 0评论

折腾:

【调研】模拟浏览器进行点击再购物等操作

期间,去试试

【记录】尝试用PhantomJS去模拟浏览器操作

参考:

Python爬虫利器五之Selenium的用法 | 静觅

去:

Downloads

现在最新的Selenium已经到3.7.1了。

Python版本的Selenium的下载:

selenium 3.7.0 : Python Package Index

Selenium Client Driver — Selenium 3.7 documentation

去安装结果出错:

pip install -U selenium

出错:

➜  自动下单 pip install -U selenium

Collecting selenium

  Downloading selenium-3.7.0-py2.py3-none-any.whl (935kB)

    40% |█████████████                   | 378kB 3.5kB/s eta 0:02:40Exception:

Traceback (most recent call last):

  File “/usr/local/lib/python2.7/site-packages/pip/basecommand.py”, line 215, in main

    status = self.run(options, args)

  File “/usr/local/lib/python2.7/site-packages/pip/commands/install.py”, line 335, in run

    wb.build(autobuilding=True)

  File “/usr/local/lib/python2.7/site-packages/pip/wheel.py”, line 749, in build

    self.requirement_set.prepare_files(self.finder)

  File “/usr/local/lib/python2.7/site-packages/pip/req/req_set.py”, line 380, in prepare_files

    ignore_dependencies=self.ignore_dependencies))

  File “/usr/local/lib/python2.7/site-packages/pip/req/req_set.py”, line 620, in _prepare_file

    session=self.session, hashes=hashes)

  File “/usr/local/lib/python2.7/site-packages/pip/download.py”, line 821, in unpack_url

    hashes=hashes

  File “/usr/local/lib/python2.7/site-packages/pip/download.py”, line 659, in unpack_http_url

    hashes)

  File “/usr/local/lib/python2.7/site-packages/pip/download.py”, line 882, in _download_http_url

    _download_url(resp, link, content_file, hashes)

  File “/usr/local/lib/python2.7/site-packages/pip/download.py”, line 603, in _download_url

    hashes.check_against_chunks(downloaded_chunks)

  File “/usr/local/lib/python2.7/site-packages/pip/utils/hashes.py”, line 46, in check_against_chunks

    for chunk in chunks:

  File “/usr/local/lib/python2.7/site-packages/pip/download.py”, line 571, in written_chunks

    for chunk in chunks:

  File “/usr/local/lib/python2.7/site-packages/pip/utils/ui.py”, line 139, in iter

    for x in it:

  File “/usr/local/lib/python2.7/site-packages/pip/download.py”, line 560, in resp_read

    decode_content=False):

  File “/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py”, line 357, in stream

    data = self.read(amt=amt, decode_content=decode_content)

  File “/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py”, line 324, in read

    flush_decoder = True

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py”, line 35, in __exit__

    self.gen.throw(type, value, traceback)

  File “/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py”, line 246, in _error_catcher

    raise ReadTimeoutError(self._pool, None, ‘Read timed out.’)

ReadTimeoutError: HTTPSConnectionPool(host=’pypi.python.org‘, port=443): Read timed out.

去看看版本:

➜  自动下单 python –version

Python 2.7.13

换上全局代理后再试试:

pip install -U selenium

还是出错:

【已解决】Mac中安装selenium出错:OSError Errno 13 Permission denied /usr/local/selenium

接着去解决:

【已解决】Mac中PyCharm中选用Python3结果:ModuleNotFoundError: No module named selenium

接着去写代码,调试看看效果。

结果又遇到:

【已解决】Mac中PyCharm调试selenium出错:selenium.common.exceptions.WebDriverException chromedriver executable needs to be in PATH

通过chrome发现百度的输入框的name=wd

所以去参考:

http://cuiqingcai.com/2599.html

试试

试了半天,关于assert和文件编码,都是弄了半天都搞定:

(1)文件编码:

【已解决】Mac中PyCharm中Python代码调试报错:SyntaxError Non-ASCII character \xe7 in file on line but no encoding

(2)asset

asset的话,对于此处中文的百度,要写成:

assert u”百度” in driver.title

才能最后asset成功:

因为此处的title本身是:

u’百度一下,你就知道’

所以要

assert u”百度” in driver.title

才能匹配到。

然后打印出来的百度搜索结果页面的html,内容很多:

然后可以看到浏览器的自动化操作的过程

  • 显示打开百度首页

  • 然后输入了crifan

  • 然后回车触发页面搜索

  • 显示出搜索结果

然后再继续研究,如何实现,找到对应的按钮,并点击按钮:

【已解决】Python中Selenium中如何实现模拟点击按钮

然后后续值得参考的帖子:

Downloads

其中解释了:各种Selenium自带的driver和第三方,包括Chrome的driver

中文:

1. 安装 — Selenium-Python中文文档 2 documentation

英文:

1. Installation — Selenium Python Bindings 2 documentation

然后继续去添加截图功能:

【已解决】Selenium中实现给浏览器截图并保存

然后看到:

Taking Screenshot Of A Page | Selenium Cooked With Python and Spiced With Java

去试试:Chrome的driver是否有窗口最大化

试了试:

driver.maximize_window()

或:

chromeOptions = webdriver.ChromeOptions()

chromeOptions.add_argument(“start-maximized”)

driver = webdriver.Chrome(chrome_options=chromeOptions)

结果都没有效果,窗口还是之前大小:

所以先不去管窗口最大化了。

【已解决】Python读取和解析配置文件作为配置选项

【已解决】Python中如何递归的删除整个非空文件夹

然后遇到了:

【已解决】Selenium给输入框send_keys无法输入文字

【已解决】Selenium已通过ID获得元素后click出错:element is not attached to the page document

接着再去:

【已解决】Selenium如何跳转到新打开的tab页窗口

转载请注明:在路上 » 【记录】尝试用Python操作PhantomJS+Selenium去模拟购物操作

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.412 seconds, using 22.11MB memory