python - Flask如何提取文章中的图片显示在首页?
PHP中文网
PHP中文网 2017-04-18 09:57:53
0
0
880

我用的是CKEditor编辑器的上传功能做的向文章中加入图片,图片是上传到本地的。
那么问题来了,我该通过什么方法获取该文章中的图片呢?

前端我是通过js进行滚动的


我尝试了下用Python库newspaper3k,只可以抓取其他网站上的内容而无法抓取本地...


main.route('/newspaper') def newspaper(): articles = [] # category = Category.query.filter_by(tag='最新消息').first() # posts = Post.query.filter_by(category=category).order_by(Post.timestamp.desc()).limit(5) # 可以抓取到内容 article = extract_article('http://www.xcb.sdnu.edu.cn/info/1014/11211.htm') # 无法抓取到内容 article = extract_article('http://127.0.0.1:5000/post/132') articles.append(article) """ for post in posts: # URL 不对 post_url = 'http://127.0.0.1:5000/post/' + str(post.id) article = extract_article(post_url=post_url) if article: article['post_url'] = post_url articles.append(article) """ return render_template('newspaperDemo.html', articles=articles) def extract_article(post_url): article = Article(post_url) print(post_url) print('-------------------------------') article.download() print('====================================') article.parse() print('*******************************') title = article.title img = article.top_image return { 'title': title, 'img': img, }

 


//photo var curIndex=0; //时间间隔(单位毫秒),每3s显示一张。 var timeInterval=3000; var arr1=new Array(); arr1[0]="../static/img/1.jpg"; arr1[1]="../static/img/2.jpg"; arr1[2]="../static/img/3.jpg"; arr1[3]="../static/img/4.jpg"; arr1[4]="../static/img/6.jpg"; var arr2=new Array(); arr2[0]="#"; arr2[1]="#"; arr2[2]="#"; arr2[3]="#"; arr2[4]="#"; setInterval(changeImg,timeInterval); function changeImg() { var obj1=document.getElementById("image"); var obj2=document.getElementById("chain"); if (curIndex==arr1.length-1) { curIndex=0; } else { curIndex+=1; } obj1.src=arr1[curIndex]; obj2.href=arr2[curIndex]; }

def index(): # 照片展示 category = Category.query.filter_by(tag='照片展示').first() post = Post.query.filter_by(category=category).order_by(Post.timestamp.desc()).limit(5) return render_template('index.html', posts=posts)
PHP中文网
PHP中文网

认证0级讲师

全部回覆 (0)
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!