游戏计划是提取那些主要图像,并在索引页中以缩略图显示它们。我在这个功能上遇到了很多麻烦,似乎在互联网上没有这个功能的例子。 我找到了三个选项 1. beautifulsoup//似乎人们最常使用这种方法,但我不知道 beautifulsoup 如何找到具有代表性的图像……而且我认为它需要最多的工作。 2. python goose//这看起来是合法的。文档说它提取主图像,我想我需要相信他们的话。问题是我不知道如何在 Django 中使用它。 3. embedly//....可能是我需要的功能的错误选择。我正在考虑为这个项目使用 python goose。 我的问题是您将如何处理这个问题?你知道任何例子或者可以提供一些我可以看的例子吗?为了从用户提供给我的页面的图像中提取图像,我可能可以使用 sorl-thumbnail(对吗?_)但是对于发布的链接....??
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/ 博客示例使用的是 flask,我尝试为使用 django 的人制作脚本
编辑 2:好的,这是我的方法。我真的认为这是对的,但不幸的是它没有给我任何东西。没有错误或没有图像,但 python 语法是正确的....如果有人为什么它不起作用请告诉我
模型.py
类(class)职位(模型。模型): 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%}
最佳答案
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/ 上托管了一个网络服务器
要输入站点,请执行 http://localhost:5000/yoursitehere ,例如 http://localhost:5000/www.google.com
关于python - 从发布的链接/和发布的页面中提取主图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455919/