我首先建立了一个基本的flask应用程序,并在Heroku上进行了部署。工作正常。将代码推送到我使用的地方后,Selenium给出easyprocess.EasyProcessError: start error <EasyProcess cmd_param=['Xvfb', '-help'] cmd=['Xvfb', '-help'] oserror=[Errno 2] No such file or directory: 'Xvfb': 'Xvfb' return_code=None stdout="None" stderr="None" timeout_happened=False>
,应用程序崩溃。
main.py
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from pyvirtualdisplay import Display
from flask import Flask
import os
import json
app = Flask(__name__)
SELENIUM_DRIVER_ARGUMENTS=['-headless']
display = Display(visible=0, size=(800, 600))
display.start()
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'geckodriver')
def __init__(self,email):
email=email
def driver(urls):
browser = webdriver.Firefox(executable_path=filename)
browser.get(urls)
return browser
def logingToAccount(start_urls,emailParameter,email,passwordParameter,xpath , byIdOrName):
browser=driver(start_urls)
if (byIdOrName=='id'):
browser.find_element_by_id(emailParameter).send_keys(email)
browser.find_element_by_id(passwordParameter).send_keys('123456')
elif(byIdOrName=='name'):
browser.find_element_by_name(emailParameter).send_keys(email)
browser.find_element_by_name(passwordParameter).send_keys('123456')
browser.find_element_by_xpath(xpath).submit()
wait = WebDriverWait(browser, 10)
return browser
def readMessage(browser,xpath,spider,loginValidMessage,loginInvalidMessage,loginValidMessageOptional1=None,
loginValidMessageOptional2=None,loginValidMessageOptional3=None,loginValidMessageOptional4=None):
try:
message = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, xpath))).text
# print(message)
if(message==loginValidMessage or message==loginValidMessageOptional1 or message==loginValidMessageOptional2
or message==loginValidMessageOptional3 or message==loginValidMessageOptional4):
# output='%s :Yes'%(spider)
# print(output)
print(spider,': Yes')
return spider,"Yes"
elif(message==loginInvalidMessage):
# output='%s :No'%(spider)
# print(output)
print(spider,': No')
return spider,"No"
# return output
finally:
browser.quit()
def facebookSpider(email):
start_urls = 'https://www.facebook.com'
browser=logingToAccount(start_urls,'email',email,'pass','//*[@id="loginbutton"]','id')
return readMessage(browser,'//*[@id="globalContainer"]/div[3]/div/div/div','Facebook','The password that you\'ve entered is incorrect. Forgotten password?','The email address that you\'ve entered doesn\'t match any account. Sign up for an account.')
def linkedInSpider(email):
start_urls = 'https://www.linkedin.com/uas/login?trk=nav_header_signin'
browser=logingToAccount(start_urls,'username',email,'password','//*[@id="app__container"]/main/div/form/div[3]/button','id')
if(browser.find_element_by_xpath('//*[@id="error-for-password"]').text):
print("LinkedIn:Yes")
return "LinkedIn","Yes"
elif(browser.find_element_by_xpath('//*[@id="error-for-username"]').text):
print("LinkedIn:No")
return "LinkedIn","No"
browser.quit()
def twitterSpider(email):
start_urls = 'https://twitter.com/account/begin_password_reset'
browser=driver(start_urls)
browser.find_element_by_name('account_identifier').send_keys(email)
return readMessage(browser,'/html/body/div[2]/div/p','Twitter','We found the following information associated with your account.','Please try searching for your email, phone number or username again.')
def instagramSpider(email):
start_urls = 'https://www.instagram.com/accounts/login/?hl=en'
browser=logingToAccount(start_urls,'username',email,'password','//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/button/div','name')
return readMessage(browser,'//*[@id="slfErrorAlert"]','Instagram','Sorry, your password was incorrect. Please double-check your password.','The username you entered doesn\'t belong to an account. Please check your username and try again.')
def pinterestSpider(email):
start_urls ='https://www.pinterest.com/'
browser=logingToAccount(start_urls,'email',email,'password','/html/body/div[1]/div/div/div/div/div/div[3]/div/div[1]/div/div/div[1]/div/div/div[1]/div[4]/div/div[1]/form/div[3]/button/div','id')
return readMessage(browser,'/html/body/div[1]/div/div/div/div/div/div[3]/div/div[1]/div/div/div[1]/div/div/div[1]/div[4]/div/div[1]/form/div[2]/fieldset/div/label/div/div','Pinterest','Hmm...that password isn\'t right. We sent you an email to help you log in','Please make a stronger password.',loginValidMessageOptional1='The password you entered is incorrect. Try again or Reset your password',loginValidMessageOptional2='Sorry, your password was incorrect. Please double-check your password.',loginValidMessageOptional3='Your account is already linked to Facebook. To log in with email, set a password.',loginValidMessageOptional4='Oops, that email’s taken or your password’s incorrect. Reset it?')
def wordpressSpider(email):
start_urls ='https://wordpress.com/log-in'
browser=driver(start_urls)
element=browser.find_element_by_name('usernameOrEmail').send_keys(email)
try:
if(WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="primary"]/div/main/div/div[1]/div/form/div[1]/div[1]/div[1]/span')))):
return "Wordpress","No"
except TimeoutException:
return "Wordpress","Yes"
finally:
browser.quit()
def fetch(email):
spiderArray=[facebookSpider(email)]
result=[]
for spider in spiderArray:
name,value=spider
result.append([name,value])
print(result)
return result
@app.route('/getinfo/<email>')
def hello_world(email):
return email
if __name__ == '__main__':
app.run()
运行
heroku logs
给出
2019-04-02T10:36:41.338046+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-02T10:36:41.338047+00:00 app[web.1]:
2019-04-02T10:36:41.338049+00:00 app[web.1]: Traceback (most recent call last):
2019-04-02T10:36:41.338051+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 180, in check_installed
2019-04-02T10:36:41.338052+00:00 app[web.1]: self.call()
2019-04-02T10:36:41.338054+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 196, in call
2019-04-02T10:36:41.338056+00:00 app[web.1]: self.start().wait(timeout=timeout)
2019-04-02T10:36:41.338057+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 232, in start
2019-04-02T10:36:41.338059+00:00 app[web.1]: raise EasyProcessError(self, 'start error')
2019-04-02T10:36:41.338074+00:00 app[web.1]: easyprocess.EasyProcessError: start error <EasyProcess cmd_param=['Xvfb', '-help'] cmd=['Xvfb', '-help'] oserror=[Errno 2] No such file or directory: 'Xvfb': 'Xvfb' return_code=None stdout="None" stderr="None" timeout_happened=False>
2019-04-02T10:36:41.338076+00:00 app[web.1]:
2019-04-02T10:36:41.338077+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-02T10:36:41.338079+00:00 app[web.1]:
2019-04-02T10:36:41.338081+00:00 app[web.1]: Traceback (most recent call last):
2019-04-02T10:36:41.338083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-04-02T10:36:41.338085+00:00 app[web.1]: worker.init_process()
2019-04-02T10:36:41.338086+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-04-02T10:36:41.338088+00:00 app[web.1]: self.load_wsgi()
2019-04-02T10:36:41.338089+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-04-02T10:36:41.338091+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2019-04-02T10:36:41.338093+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-04-02T10:36:41.338095+00:00 app[web.1]: self.callable = self.load()
2019-04-02T10:36:41.338096+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-04-02T10:36:41.338098+00:00 app[web.1]: return self.load_wsgiapp()
2019-04-02T10:36:41.338100+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-04-02T10:36:41.338101+00:00 app[web.1]: return util.import_app(self.app_uri)
2019-04-02T10:36:41.338103+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2019-04-02T10:36:41.338104+00:00 app[web.1]: __import__(module)
2019-04-02T10:36:41.338106+00:00 app[web.1]: File "/app/app.py", line 15, in <module>
2019-04-02T10:36:41.338108+00:00 app[web.1]: display = Display(visible=0, size=(800, 600))
2019-04-02T10:36:41.338110+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/pyvirtualdisplay/display.py", line 34, in __init__
2019-04-02T10:36:41.338111+00:00 app[web.1]: self._obj = self.display_class(
2019-04-02T10:36:41.338113+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/pyvirtualdisplay/display.py", line 52, in display_class
2019-04-02T10:36:41.338115+00:00 app[web.1]: cls.check_installed()
2019-04-02T10:36:41.338116+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/pyvirtualdisplay/xvfb.py", line 38, in check_installed
2019-04-02T10:36:41.338118+00:00 app[web.1]: ubuntu_package=PACKAGE).check_installed()
2019-04-02T10:36:41.338120+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 182, in check_installed
2019-04-02T10:36:41.338122+00:00 app[web.1]: raise EasyProcessCheckInstalledError(self)
2019-04-02T10:36:41.338124+00:00 app[web.1]: easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help']
2019-04-02T10:36:41.338125+00:00 app[web.1]: OSError=[Errno 2] No such file or directory: 'Xvfb': 'Xvfb'
2019-04-02T10:36:41.338179+00:00 app[web.1]: Program install error!
2019-04-02T10:36:41.339371+00:00 app[web.1]: [2019-04-02 10:36:41 +0000] [10] [INFO] Worker exiting (pid: 10)
2019-04-02T10:36:41.347446+00:00 app[web.1]: [2019-04-02 10:36:41 +0000] [11] [ERROR] Exception in worker process
2019-04-02T10:36:41.347451+00:00 app[web.1]: Traceback (most recent call last):
2019-04-02T10:36:41.347457+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 227, in start
2019-04-02T10:36:41.347459+00:00 app[web.1]: env=self.env,
2019-04-02T10:36:41.347461+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/subprocess.py", line 729, in __init__
2019-04-02T10:36:41.347462+00:00 app[web.1]: restore_signals, start_new_session)
2019-04-02T10:36:41.347464+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/subprocess.py", line 1364, in _execute_child
2019-04-02T10:36:41.347466+00:00 app[web.1]: raise child_exception_type(errno_num, err_msg, err_filename)
2019-04-02T10:36:41.347467+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: 'Xvfb': 'Xvfb'
2019-04-02T10:36:41.347469+00:00 app[web.1]:
2019-04-02T10:36:41.347471+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-02T10:36:41.347472+00:00 app[web.1]:
2019-04-02T10:36:41.347474+00:00 app[web.1]: Traceback (most recent call last):
2019-04-02T10:36:41.347476+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 180, in check_installed
2019-04-02T10:36:41.347478+00:00 app[web.1]: self.call()
2019-04-02T10:36:41.347479+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 196, in call
2019-04-02T10:36:41.347481+00:00 app[web.1]: self.start().wait(timeout=timeout)
2019-04-02T10:36:41.347483+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 232, in start
2019-04-02T10:36:41.347484+00:00 app[web.1]: raise EasyProcessError(self, 'start error')
2019-04-02T10:36:41.347486+00:00 app[web.1]: easyprocess.EasyProcessError: start error <EasyProcess cmd_param=['Xvfb', '-help'] cmd=['Xvfb', '-help'] oserror=[Errno 2] No such file or directory: 'Xvfb': 'Xvfb' return_code=None stdout="None" stderr="None" timeout_happened=False>
2019-04-02T10:36:41.347488+00:00 app[web.1]:
2019-04-02T10:36:41.347489+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-02T10:36:41.347491+00:00 app[web.1]:
2019-04-02T10:36:41.347492+00:00 app[web.1]: Traceback (most recent call last):
2019-04-02T10:36:41.347494+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-04-02T10:36:41.347496+00:00 app[web.1]: worker.init_process()
2019-04-02T10:36:41.347497+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-04-02T10:36:41.347499+00:00 app[web.1]: self.load_wsgi()
2019-04-02T10:36:41.347500+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-04-02T10:36:41.347502+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2019-04-02T10:36:41.347504+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-04-02T10:36:41.347505+00:00 app[web.1]: self.callable = self.load()
2019-04-02T10:36:41.347507+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-04-02T10:36:41.347510+00:00 app[web.1]: return self.load_wsgiapp()
2019-04-02T10:36:41.347519+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-04-02T10:36:41.347521+00:00 app[web.1]: return util.import_app(self.app_uri)
2019-04-02T10:36:41.347523+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2019-04-02T10:36:41.347524+00:00 app[web.1]: __import__(module)
2019-04-02T10:36:41.347525+00:00 app[web.1]: File "/app/app.py", line 15, in <module>
2019-04-02T10:36:41.347527+00:00 app[web.1]: display = Display(visible=0, size=(800, 600))
2019-04-02T10:36:41.347529+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/pyvirtualdisplay/display.py", line 34, in __init__
2019-04-02T10:36:41.347530+00:00 app[web.1]: self._obj = self.display_class(
2019-04-02T10:36:41.347532+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/pyvirtualdisplay/display.py", line 52, in display_class
2019-04-02T10:36:41.347533+00:00 app[web.1]: cls.check_installed()
2019-04-02T10:36:41.347537+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/pyvirtualdisplay/xvfb.py", line 38, in check_installed
2019-04-02T10:36:41.347539+00:00 app[web.1]: ubuntu_package=PACKAGE).check_installed()
2019-04-02T10:36:41.347541+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/easyprocess/__init__.py", line 182, in check_installed
2019-04-02T10:36:41.347546+00:00 app[web.1]: raise EasyProcessCheckInstalledError(self)
2019-04-02T10:36:41.347548+00:00 app[web.1]: easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help']
2019-04-02T10:36:41.347550+00:00 app[web.1]: OSError=[Errno 2] No such file or directory: 'Xvfb': 'Xvfb'
2019-04-02T10:36:41.347598+00:00 app[web.1]: Program install error!
2019-04-02T10:36:41.349005+00:00 app[web.1]: [2019-04-02 10:36:41 +0000] [11] [INFO] Worker exiting (pid: 11)
2019-04-02T10:36:41.541056+00:00 app[web.1]: [2019-04-02 10:36:41 +0000] [4] [INFO] Shutting down: Master
2019-04-02T10:36:41.541219+00:00 app[web.1]: [2019-04-02 10:36:41 +0000] [4] [INFO] Reason: Worker failed to boot.
2019-04-02T10:36:41.650464+00:00 heroku[web.1]: State changed from up to crashed
2019-04-02T10:36:41.633720+00:00 heroku[web.1]: Process exited with status 3
2019-04-02T10:36:41.000000+00:00 app[api]: Build succeeded
2019-04-02T10:36:46.123898+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/getinfo/afda" host=young-hollows-53719.herokuapp.com request_id=b37acb71-6671-4737-9e86-ee8bab0d40f3 fwd="202.69.200.82" dyno= connect= service= status=503 bytes= protocol=https
(venv) kabilesh@kabilesh-Latitude-E6540:~/PycharmProjects/HerokuSpider$
为什么会出现此错误?导入Firefox驱动程序是否有问题?我该如何解决?在本地运行时,可以正常运行。