日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python网址在浏览器能打开_Python - Flask - 在默认浏览器中打开一个网页

發布時間:2024/9/15 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python网址在浏览器能打开_Python - Flask - 在默认浏览器中打开一个网页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一部分負責抓取網頁并提取一些信息并將其插入到數據庫中。

第二部分是resposible使用數據庫來呈現這些信息。 兩部分共享數據庫。在第二部分中,我使用Flask框架將信息顯示為具有格式,樣式等的html,以使其看起來更清晰。

兩個部分的源文件都在同一個包,但運行此程序正常用戶有權運行履帶式并分別導致主持人是這樣的:

蟒蛇crawler.py

然后

蟒蛇presenter.py

一切都還好吧只是,除了一兩件事。我主要做什么是以html格式創建結果并在用戶的默認瀏覽器中打開結果頁面,但它總是打開兩次,可能是由于run()方法的存在,它會在新線程中啟動Flask并且事情變得對我來說陰天。我不知道該怎么做才能讓我的presenter.py在運行后只打開一個選項卡/窗口。

這里是我的代碼片段:

from flask import Flask, render_template

import os

import sqlite3

# configuration

DEBUG = True

DATABASE = os.getcwd() + '/database/database.db'

app = Flask(__name__)

app.config.from_object(__name__)

app.config.from_envvar('CRAWLER_SETTINGS', silent=True)

def connect_db():

"""Returns a new connection to the database."""

try:

conn = sqlite3.connect(app.config['DATABASE'])

return conn

except sqlite3.Error:

print 'Unable to connect to the database'

return False

@app.route('/')

def show_entries():

u"""Loads pages information and emails from the database and

inserts results into show_entires template. If there is a database

problem returns error page.

"""

conn = connect_db()

if conn:

try:

cur = connect_db().cursor()

results = cur.execute('SELECT url, title, doctype, pagesize FROM pages')

pages = [dict(url=row[0], title=row[1].encode('utf-8'), pageType=row[2], pageSize=row[3]) for row in results.fetchall()]

results = cur.execute('SELECT url, email from emails')

emails = {}

for row in results.fetchall():

emails.setdefault(row[0], []).append(row[1])

return render_template('show_entries.html', pages=pages, emails=emails)

except sqlite3.Error, e:

print ' Exception message %s ' % e

print 'Could not load data from the database!'

return render_template('show_error_page.html')

else:

return render_template('show_error_page.html')

if __name__ == '__main__':

url = 'http://127.0.0.1:5000'

webbrowser.open_new(url)

app.run()

2012-06-20

koleS

總結

以上是生活随笔為你收集整理的python网址在浏览器能打开_Python - Flask - 在默认浏览器中打开一个网页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。