Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fancy.py #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: gunicorn fancy:app
web: gunicorn fancy:app -t 3000
27 changes: 17 additions & 10 deletions fancy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urllib.request
import http.cookiejar
from bs4 import BeautifulSoup
import re
import re, os
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from pymongo import *
Expand Down Expand Up @@ -76,25 +76,32 @@ def get_info(links):
print(item, ' processed')
i+=1
print(i, 'done,',len(links) - i ,'remaining')
if i >= 20:
return itemlist
print('信息收集完毕')
return itemlist

@app.before_first_request
def update():
# client = MongoClient('mongodb://uPvyEVzH4jIWGhcn:[email protected]:27017/')
client = MongoClient()
db = client.test
client = MongoClient(os.environ.get('DATABASE_URL'))
db = client.fanci
item_collection = db.item
itemlist = get_info(get_links())
item_collection.insert_many(itemlist)
if not item_collection.find_one():
itemlist = get_info(get_links())
item_collection.insert_many(itemlist)
print('信息已写入数据库')
else:
print('数据库已存在信息')

@app.route('/')
def index():
# client = MongoClient('mongodb://uPvyEVzH4jIWGhcn:[email protected]:27017/')
client = MongoClient()
db = client.test
client = MongoClient(os.environ.get('DATABASE_URL'))
# client = MongoClient()
db = client.fanci
item_collection = db.item
items = item_collection.find()
print('正从数据库取出信息')
return render_template('index.html', items = items)

if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0', port = 3000)
app.run(debug=True,host='0.0.0.0', port = 3000)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ itsdangerous==0.24
pymongo==3.2.2
redis==2.10.5
visitor==0.1.2
gunicorn==19.6.0