python selenium webdriver_Python:selenium.webdriver问题(服务器上没有X)
我拼湊了以下腳本,以在遠(yuǎn)程Asterisk / Vicidial服務(wù)器上生成每日?qǐng)?bào)告.該腳本從報(bào)告中獲取源代碼,執(zhí)行某種格式設(shè)置,將結(jié)果保存為文本文件,然后通過smtp發(fā)送給我的老板以供審核.我目前正在本地的cron作業(yè)上運(yùn)行腳本,并且腳本運(yùn)行良好,但是我希望能夠在VPS上運(yùn)行該腳本;問題是,該腳本會(huì)彈出Firefox窗口,并且VPS沒有X或任何類型的GUI,因此Firefox無法打開,并且無法檢索數(shù)據(jù).
需要澄清的是,我已經(jīng)安裝了Firefox和所有必需的模塊,并且VPS本質(zhì)上與我的本地計(jì)算機(jī)(bar X和GUI(Debian Lenny))相同.
如果有人可以提供有關(guān)如何修改此腳本的任何幫助,以使其在沒有X / GUI的情況下工作,將不勝感激!
謝謝,托比.
import contextlib
import selenium.webdriver as webdriver
import lxml.html as LH
import lxml.html.clean as clean
import csv
import sys
import smtplib
from email.mime.text import MIMEText
import email.mime.application
import email
import mimetypes
import datetime
date=datetime.date.today()
url="http://myuser:mypass@ipaddress"+ str(date) + "some_other_string"
ignore_tags=('script','noscript','style')
with contextlib.closing(webdriver.Firefox()) as browser:
browser.get(url)
content=browser.page_source
cleaner=clean.Cleaner()
content=cleaner.clean_html(content)
with open('vicidial_data.html','w') as f:
f.write(content.encode('utf-8'))
doc=LH.fromstring(content)
with open('grab_raw.txt','w') as f:
for elt in doc.iterdescendants():
if elt.tag in ignore_tags: continue
text=elt.text or ''
tail=elt.tail or ''
words=' '.join((text,tail)).strip()
if words:
words=words.encode('utf-8')
f.write(words+'
')
grab=open( 'grab_raw.txt', 'r' )
grab_list=grab.readlines()
grab.close()
del grab_list[0:21]
grab_out=open("Vicidial_Report-"+str(date)+".txt", 'w')
grab_out.writelines(grab_list)
grab_out.close()
msg=email.mime.Multipart.MIMEMultipart()
msg['Subject']='Vicidial call-report'
msg['From']='me@mycomapny.com'
msg['To']='myboss@mycompany.com'
body = email.mime.Text.MIMEText("Please find attached call-report for " + str(date))
msg.attach(body)
filename= "Vicidial_Report-"+str(date)+".txt"
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="text")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)
s = smtplib.SMTP('smtp.gmail.com')
s.starttls()
s.login('mygmaillogin@mycompanydomain.com','mypassword')
s.sendmail('me@mycompanydomain.com',['myboss@mycompanydomain.com', 'someoneelse@mycompanydomain.com', ], msg.as_string())
s.quit()
總結(jié)
以上是生活随笔為你收集整理的python selenium webdriver_Python:selenium.webdriver问题(服务器上没有X)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: window.open 不显示地址栏_谷
- 下一篇: sqlserver concat 字符串