【问题标题】:Extracting main image from posted link/ and from the posted page从发布的链接/和发布的页面中提取主图像
【发布时间】:2015-12-24 17:08:48
【问题描述】:

游戏计划是提取那些主要图像,并在索引页面中以缩略图的形式显示它们。我在这个功能上遇到了很多麻烦,互联网上似乎没有这个功能的例子。 我找到了三个选项 1. beautifulsoup// 似乎人们使用这种方法最多,但我不知道beautifulsoup 是如何找到代表图像的……而且我认为它需要的工作最多。 2. python goose//这看起来是合法的。文档说它提取主图像,我想我需要相信他们的话。问题是我不知道如何在 django 中使用它。 3. embedly//....我需要的功能可能是错误的选择。我正在考虑为这个项目使用 python goose。 我的问题是您将如何解决这个问题?你知道任何例子或者可以提供一些我可以看的例子吗?为了从用户提供给我的页面的图像中提取图像,我可能可以使用 sorl-thumbnail(right?_) 但对于发布的链接....??

Edit1:使用python goose,看起来(主)图像抓取非常简单。问题是我不确定如何将脚本用于我的应用程序,我应该如何将该图像转换为右侧缩略图并显示在我的 index.html 上... 这是我的 media.py(不确定它是否有效

  import json
from goose import Goose

def extract(request):
    url = request.args.get('url')
    g = Goose()
    article = g.extract(url=url)
    resposne = {'image':article.top_image.src}
    return json.dumps(resposne)

来源:https://blog.openshift.com/day-16-goose-extractor-an-article-extractor-that-just-works/ 博客示例使用的是烧瓶,我尝试为使用 django 的人制作脚本

编辑 2:好的,这是我的方法。我真的认为这是对的,但不幸的是它没有给我任何东西。没有错误或没有图像,但 python 语法是正确的....如果有人为什么它不起作用,请告诉我

模型.py

类帖子(models.Model): url = models.URLField(max_length=250, blank=True, null=True)

def extract(request, url):
    url = requests.POST.get('url')
    g = Goose()
    article = g.extract(url=url)
    resposne = {'image':article.top_image.src}
    return json.dumps(resposne) 

索引.html

{% if posts %}
    {% for post in posts %}
      {{ post.extract}}
{%endfor%}
{%endif%}

【问题讨论】:

    标签: python django


    【解决方案1】:

    BeautifulSoup 将是实现这一目标的方法,而且实际上非常简单。

    首先,HTML 中的图像如下所示:

    <img src="http://www.url.to/image.png"></img>
    

    我们可以使用 BeautifulSoup 提取所有的img 标签,然后找到img 标签的src。如下图所示。

    from bs4 import BeautifulSoup #Import stuff
    import requests
    
    r  = requests.get("http://www.site-to-extract.com/") #Download website source
    
    data = r.text  #Get the website source as text
    
    soup = BeautifulSoup(data) #Setup a "soup" which BeautifulSoup can search
    
    links = []
    
    for link in soup.find_all('img'):  #Cycle through all 'img' tags
        imgSrc = link.get('src')   #Extract the 'src' from those tags
        links.append(imgSrc)    #Append the source to 'links'
    
    print(links)  #Print 'links'
    

    我不知道您打算如何决定将哪张图片用作缩略图,但您可以通过 URL 列表提取您想要的。

    更新

    我知道你说的是 dJango,但我强烈推荐 Flask。它更简单,但仍然非常实用。

    我写了这个,它只是显示你给它的任何网页的第一张图片。

    from bs4 import BeautifulSoup #Import stuff
    import requests
    from flask import Flask
    app = Flask(__name__)
    
    def getImages(url):
        r  = requests.get(url) #Download website source
    
        data = r.text  #Get the website source as text
    
        soup = BeautifulSoup(data) #Setup a "soup" which BeautifulSoup can search
    
        links = []
    
        for link in soup.find_all('img'):  #Cycle through all 'img' tags
            imgSrc = link.get('src')   #Extract the 'src' from those tags
            links.append(imgSrc)    #Append the source to 'links'
    
        return links  #Return 'links'
    
    @app.route('/<site>')
    def page(site):
        image = getImages("http://" + site)[0] #Here I find the 1st image on the page
        if image[0] == "/":
            image = "http://" + site + image  #This creates a URL for the image
        return "<img src=%s></img>" % image  #Return the image in an HTML "img" tag
    
    if __name__ == '__main__':
        app.run(debug=True, host="0.0.0.0")  #Run the Flask webserver
    

    这会在 http://localhost:5000/ 上托管一个 Web 服务器

    要输入网站,请输入http://localhost:5000/yoursitehere,例如http://localhost:5000/www.google.com

    【讨论】:

    • 我只是在做这个,我决定使用 python goose 并且在将脚本实现到我的 index.html 时遇到问题...我将编辑我所做的,如果你可以看看。
    • 那么您的代码不会打印所有图像吗?不只是一张主图?这就是我改用python goose的原因
    • @haloyoba “主图像”是什么意思?比如,页面上最大的图片,或者页面加载时的实际样子?
    • 是的,代表文章的图片,可能是最大的图片
    • @haloyoba - 好的,所以您需要做的就是浏览图像 URL 列表并确定哪个合适,可能通过检查它的尺寸。不过我真的不知道你会怎么做。
    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多