python的flask前端显示图片_Flask如何提取文章中的图片显示在首页?

我用的是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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值