在Django模板中显示抓取的结果

问题描述 投票:2回答:3

我正在测试使用django建立一个抓取站点。出于某种原因,以下代码仅提供一张图片,我希望它打印每张图片,每个链接和每个价格,有帮助吗? (同样,如果你们知道如何将这些数据放入数据库模型中,这样我就不必总是抓取该站点,我很烦,但这可能是另一个问题)干杯!

这里是模板文件:

{% extends "base.html" %}

{% block title %}Boats{% endblock %}

{% block content %}

<img src="{{ fetch_boats }}"/>

{% endblock %}

这是views.py文件:

#views.py
from django.shortcuts import render_to_response
from django.template.loader import get_template
from django.template import Context
from django.http import Http404, HttpResponse
from fetch_images import fetch_imagery

def fetch_it(request):
    fi = fetch_imagery()
    return render_to_response('fetch_image.html', {'fetch_boats' : fi})

这里是fetch_images模块:

#fetch_images.py
from BeautifulSoup import BeautifulSoup
import re
import urllib2

def fetch_imagery():
    response = urllib2.urlopen("http://www.boattrader.com/search-results/Type")
    html = response.read()

#create a beautiful soup object
    soup = BeautifulSoup(html)

#all boat images have attribute height=165
    images = soup.findAll("img",height="165")
    for image in images:
        return image['src'] #print th url of the image only

# all links to detailed boat information have class lfloat
    links = soup.findAll("a", {"class" : "lfloat"})
    for link in links:
        return link['href']
        #print link.string

# all prices are spans and have the class rfloat
    prices = soup.findAll("span", { "class" : "rfloat" })
    for price in prices:
        return price
        #print price.string

最后,如果需要,在urlconf中的映射的URL在下面:

from django.conf.urls.defaults import *
from mysite.views import fetch_it

urlpatterns = patterns('', ('^fetch_image/$', fetch_it))
python django django-templates screen-scraping django-views
3个回答
2
投票

您的fetch_imagery函数需要做些工作-因为您要返回(而不是使用yield),所以第一个return image['src']将终止函数调用(我在这里假设所有这些返回都是同一个函数的一部分)函数定义(如您的代码所示)。

此外,我的假设是,您将从fetch_imagery返回一个列表/元组(或定义生成器方法),在这种情况下,您的模板需要像这样:

{% block content %}
    {% for image in fetch_boats %}
        <img src="{{ image }}" />
    {% endfor %}
{% endblock %}

这基本上将遍历列表中的所有项目(在您的情况下为图像URL),并将为每个项目创建img标签。


2
投票

超出范围,但是在我看来,报废是过度的cpu时间/内存/带宽消耗,我认为应该在异步操作的后台进行。

虽然是个好主意:)


0
投票

我在网上搜索了很长时间,寻找了一个示例以显示抓取的数据,这篇文章确实很有帮助。自从问题首次发布以来,模块已经进行了一些小的更改,因此我想我应该将其更新并发布我所拥有的代码以及所需的更改。

这有一个很好的例子,它提供了一个示例,该示例演示了如何响应流量来运行一些Python代码,并生成没有任何理由不涉及数据库或模型类的简单内容。

假设您有一个可以将这些更改添加到的Django项目,您应该能够浏览到<your-base-url>/fetch_boats并看到一堆船上的图片。

views.py

import django.shortcuts
from django.shortcuts import render
from bs4 import BeautifulSoup
import urllib.request

def fetch_boats(request):
    fi = fetch_imagery()
    return render(request, "fetch_boats.html", {"fetch_boats": fi})

def fetch_imagery():
    response = urllib.request.urlopen("http://www.boattrader.com")
    html     = response.read()
    soup     = BeautifulSoup(html)
    images   = soup.findAll("img")

    for image in images:
        yield image["src"]

urls.py

from django.urls import path
from .views import fetch_boats

urlpatterns = [
    path('fetch_boats', fetch_boats, name='fetch_boats'),
]

templates / fetch_boats.html

{% extends 'base.html' %}
{% block title %} ~~~&lt; Boats &gt;~~~ {% endblock title %}
{% block content %}

    {% for image in fetch_boats %}
        <br /><br />
        <img src="{{ image }}" />
    {% endfor %}

{% endblock content %}
© www.soinside.com 2019 - 2024. All rights reserved.