gpt4 book ai didi

python - Selenium - 引发 httplib.BadStatusLine

转载 作者:行者123 更新时间:2023-11-28 22:31:31 26 4
gpt4 key购买 nike

我将 PhantomJS 与 Selenium 结合使用,我想在 stackoverflow 上进行多次搜索。这段代码在我的本地电脑上运行良好,当我将它更改为内存较少的服务器时,它会引发 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com