我将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: ''
答案 0 :(得分:4)
您看到错误是因为服务器上的内存不足,因此您需要增加内存量。我假设您的本地计算机上有更多内存,因此,它按预期运行的原因。不幸的是,PhantomJS + Selenium是内存密集型的。