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

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

<label id="workTitle"><a href="#"><b>工作状态</b></a></label>
    <p id="photo">
    <a id="chain" href="#"><img id=image width=100% height=100% border =0></a>
</p>

//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级讲师

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!