python - Selenium - 引发 httplib.BadStatusLine

标签 python selenium phantomjs

我正在使用 PhantomJS 和 Selenium,我想在 stackoverflow 上进行一些搜索。这段代码在我的本地电脑上运行良好,当我将其更改为 RAM 较少的服务器时,它会引发 httplib.BadStatusLine 错误。 这听起来像是安装问题或超出系统规范,有人可以澄清如何解决这个问题吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

start = time.time()
driver = webdriver.PhantomJS() # or add to your PATH
array = ["Python", "Javascript", "Golang", "C++", "C", "CSS", "XML", "Json", "Ruby", "Django",
     "Rails", "Java", "OpenCV", "GameMaker", "iOS", "Android", "React", "AngularJS", "React Native"]
for i in array:
    driver.get('http://stackoverflow.com/')
    sbtn = driver.find_element(By.XPATH, '//*[@id="search"]/input')
    sbtn.send_keys(i)
    sbtn.send_keys(Keys.ENTER)
    driver.save_screenshot('Photos/screen' + i + '.png') # save a screenshot to disk

错误:

Traceback (most recent call last):
File "test.py", line 14, in <module>
    driver.save_screenshot('Photos/screen' + i + '.png') # save a screenshot to disk
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 798, in get_screenshot_as_file
    png = self.get_screenshot_as_png()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 817, in get_screenshot_as_png
    return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 827, in get_screenshot_as_base64
    return self.execute(Command.SCREENSHOT)['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 408, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 478, in _request
    resp = opener.open(request, timeout=self._timeout)
  File "/usr/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1187, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 415, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 379, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

最佳答案

您看到此错误是因为服务器内存不足,因此您需要增加内存量。我假设您的本地计算机上有更多内存,因此,这就是它按预期运行的原因。不幸的是,PhantomJS + Selenium 是内存密集型的。

关于python - Selenium - 引发 httplib.BadStatusLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41528560/

相关文章:

python - 使用 imaplib 获取电子邮件,但不要将其标记为 SEEN

java - Appium/Selenium - 断言声明为字段的元素不显示

python - find_elements_by_css_selector 没有给我任何输出,如何处理?

python - 使用 Python 抓取 Ajax

javascript - 如何在 Phantom.js 中监控 WebSocket 通信?

javascript - 如何在 Javascript 中写入 CSV 文件

python - 卸载 pillow 还需要删除另外 161 个包

Python:打开带有附件的 outlook 组合实例

javascript - 有没有办法将对象传递给 casper.js 的 evaluate()?

python - 返回已排序单词的列表