def detile(itemid): zhihus = ZhihuModel.objects() for zhihu in zhihus: if(zhihu.item_id==int(itemid)): item = zhihu break return render_template('detail.html',item=item)
def hello_world(): # url = "http://news-at.zhihu.com/api/4/news/latest" # data=urllib.request.urlopen(url).read() # z_data=data.decode('UTF-8') # items = json.loads(z_data) stories = ZhihuModel.objects() return render_template('home.html',stories=stories)
def pullstories(story,date): imgurl = str(story['images'][0]) img = imgurl[22:] # imgdata=urllib.request.urlopen(imgurl).read() # print(img) # open(createFileWithFileName(localPath,img), "wb").write(imgdata) storyModel = ZhihuModel(item_id = story['id'] ) storyModel.item_date = date storyModel.item_title = story['title'] storyModel.item_frontimg = str(story['images'][0]) itemid = story['id'] url1 = "http://news-at.zhihu.com/api/4/news/"+str(itemid) itemContent = urllib.request.urlopen(url1).read() content_data=itemContent.decode('UTF-8') content = json.loads(content_data) storyModel.item_content = content['body'] storyModel.item_css = content['css'][0] storyModel.save() return storyModel
import sys,urllib,os sys.path.append('D:\\PycharmProjects\\FlaskBlog') from model import ZhihuModel from htmlparse import MyHTMLParser localPath = '..\\static\\image\\content' def createFileWithFileName(localPathParam,fileName): totalPath=localPathParam+'\\'+fileName if not os.path.exists(totalPath): file=open(totalPath,'a+') file.close() return totalPath zhihus = ZhihuModel.objects() for zhihu in zhihus: downloadImg(zhihu)