-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrans.py
29 lines (26 loc) · 857 Bytes
/
Trans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/env python3
# coding : utf-8
from collections import defaultdict
import pymongo
class News(object):
def __init__(self):
self.global_news=defaultdict(list)
self.db=self.connect_db()
def connect_db(self):
client=pymongo.MongoClient(host='自己mongodb',port=2232)
client.admin.authenticate('root','redhat')
db=client.spiders
return db
def query(self,keyword):
if self.db is not None:
collection=self.db[keyword]
else :
return None
result=collection.find().sort('date',pymongo.ASCENDING).limit(10)
for res in result:
self.global_news[keyword].append((res['title'],res['link']))
if __name__ == '__main__':
news=News()
news.query('baidunews')
info=news.global_news.get('baidunews')
print(info)