我遇到一个小问题,我想将selenium driver
和PhantomJS
拍摄的(截图)图像上传到我的网站并将其放入数据库。
我正在使用get_screenshot_as_file()
函数将图像保存到当前目录中,如果成功完成了此过程,此函数只会给我True
或False
值,但是我需要的是文件名,所以我可以将它保存在数据库中。
以下是我的观点和模型摘要:
models.py
class Driver(PhantomJS):
def __init__(self, *args, **kwargs):
super(Driver, self).__init__(*args, **kwargs)
self._log = StderrLog()
class ScreenShot(db.Model):
id = db.Column(db.Integer, primary_key=True)
image = db.Column(db.String(255), default=None)
title = db.Column(db.String(80))
describe = db.Column(db.Text)
def __init__(self, image, title, describe):
self.image = image
self.title = title
self.describe = describe
def __repr__(self):
return "Title %r" %self.title
views.py
from blog.form import ScreenShotForm
from blog.models import ScreenShot, StderrLog, Driver
@app.route('/my_works/', methods=('GET','POST'))
@only_author
def my_works():
blog = Blog.query.first()
form = ScreenShotForm()
if form.validate_on_submit():
url = request.args.get(url_for('index'), form.title.data)
width = int(request.args.get("w", 1200))
min_height = int(request.args.get("h", 400))
wait_time = float(request.args.get("t", 20)) / 1000 # ms
driver = Driver()
driver.set_window_position(0, 0)
driver.set_window_size(width, min_height)
driver.set_page_load_timeout(100)
driver.implicitly_wait(100)
driver.get(url)
driver.set_window_size(width, min_height)
time.sleep(wait_time)
sys.stderr.write(driver.execute_script("return document.readyState") + "\n")
image = driver.get_screenshot_as_file(os.urandom(3).encode('hex')+'.png')
title = form.title.data
describe = form.describe.data
scrshot = ScreenShot(image, title, describe)
db.session.add(scrshot)
db.session.commit()
driver.quit()
flash('Screenshot added.')
return redirect(url_for('index'))
#driver.get_screenshot_as_file('foo.png')
#return "<h1>Image saved</h1>"
#return Response(png, mimetype="image/png")
return render_template('blog/my_works.html', blog=blog, form=form)
form.py
class ScreenShotForm(Form):
title = StringField('Site',[validators.Required(), validators.length(max=80)])
describe = TextAreaField('Describtion', validators=[validators.Required()])
正如您所看到的,我正在使用os.urandom(3).encode('hex')+'.png'
因此它可以为我的屏幕截图提供一个随机名称,这样我就不会感到困惑,在数据库中我得到1表示True
,也在我的项目文件夹中我完全得到截图25de0a.png
文件,听起来很棒!!!!,但我不希望这种情况发生,我想获取文件名而不是True
或False
表达。
这是我在数据库中获得的代码片段:
+----+-------+-------------------------+-------------------+
| id | image | title | describe |
+----+-------+-------------------------+-------------------+
| 6 | 0 | http://www.youtube.com/ | testing testing . |
| 7 | 1 | http://www.youtube.com/ | asdasfasfwf324214 |
+----+-------+-------------------------+-------------------+
我尝试了get_screenshot_as_png()
功能,我得到的是图像的源代码。
如果结果如下,那就太好了:
+----+-------+-------------------------+-------------------+
| id | image | title | describe |
+----+-------+-------------------------+-------------------+
| 6 | 25de0a.png | http://www.youtube.com/ | testing testing . |
| 7 | 25de0a.png | http://www.youtube.com/ | asdasfasfwf324214 |
+----+-------+-------------------------+-------------------+
最终,请任何帮助吨吨吨赞赏:)。
答案 0 :(得分:1)
您将所需的文件名传递给get_screenshot_as_file
方法(正如您现在所做的那样)。如果文件成功写入,get_screenshot_as_file
方法返回True,否则返回False。
您的代码应该是:
image_filename = os.urandom(3).encode('hex')+'.png'
if driver.get_screenshot_as_file(image_filename):
# file was successfully written
title = form.title.data
describe = form.describe.data
scrshot = ScreenShot(image_filename, title, describe)
db.session.add(scrshot)
db.session.commit()
else:
# file was not written
pass