显示Django的搜索结果

时间:2017-04-12 20:01:51

标签: python django

我正在尝试为学校项目开发一个网络应用程序。基本功能是您搜索葡萄酒,脚本刮擦多个网站并返回葡萄酒名称,年份,价格以及您可以购买的链接的结果。我目前主要使用的代码是服务器将进行搜索并返回结果。但是,每次搜索新葡萄酒时,之前搜索的结果仍保留在呈现的表格中。我无法弄清楚为什么。下面是我的views.py和相关的相应模板代码:(被抓取的数据作为具有相关属性的对象字典发送)

视图

def search(request):
    return render(request, 'findwine/search.html', {})

def results(request):
    if request.method == 'POST':
        print(request)
        query = request.POST.get('search')
        search_results=WineScraper.searchfunction(query)
        return render(request, 'findwine/results.html', {'search_results': search_results})
    else:
        return render(request, 'findwine/search.html', {})

HTML结果模板

<div class="container text-center">

      <a href="{% url 'search' %}" id="search">Back to Search</a>
  </div>

  <div class="table-responsive">

      <table class="table table-bordered table-hover table-striped tablesorter">

          <thead>

          <tr>

            <th class="header"> Wine Name <i class="icon-sort"></i></th>

            <th class="header"> Vintage <i class="icon-sort"></i></th>

            <th class="header"> Price <i class="icon-sort"></i></th>

            <th class="header"> Wine Link <i class="icon-sort"></i></th>

          </tr>

      </thead>

      <tbody>


{% if search_results %}
      {% for key,value in search_results.items %}

          <tr>

              <td>{{ value.winename }}</td>

              <td>{{ value.vintage }}</td>

              <td>{{ value.price }}</td>

              <td> <a href={{ value.winelink }}>Buy Wine</a></td>

          </tr>

      {% endfor %}
{% endif %}


      </tbody>

      </table>

  </div>

</div>

HTML search template
<form class="form-signin" id="results" method="POST" action="/results/">

        {% csrf_token %}

        <br>

        <input type="text" name="search" class="form-control" placeholder="Search for Wine to Buy" value="" required autofocus>

        <br>

        <button class="btn btn-lg btn-primary btn-block" type="submit">Search</button>

      </form>

WineScraper.py

import requests
from bs4 import BeautifulSoup
import re

# variables
wine_results={}
hdr={'User-Agent':'Mozilla/5.0'}
class wine:
def __init__(self,winename,vintage,price,winelink):
    self.winename=winename
    self.vintage=vintage
    self.price=price
    self.winelink=winelink

#data manipulation functions
def getlinks(query):
i=0
list=[]
for i,item in enumerate(query):
    list.append(query[i]['href'])
    i=i+1
return list

def gettext(query):
i=0
list=[]
for i,item in enumerate(query):
    list.append(query[i].text)
    i=i+1
return list

def getyear(query):
i=0
list=[]
list1=gettext(query)
for i,item in enumerate(list1):
    match=re.search('\d{4}', list1[i])
    if match:
        list.append(match.group())
    else:
        list.append('None')
    i=i+1
return list

def getprice(query):
i=0
list=[]
list1=gettext(query)
for i,item in enumerate(list1):
    match=re.search('[1-9]\d*\.\d*|\d+\.\d*',list1[i])
    if match:
        list.append('$'+ match.group())
    else:
        list.append('None')
    i=i+1
return list

def filteryear(query):
i=0
list=[]
list1=gettext(query)
for i,item in enumerate(list1):
    match=re.sub('\d{4}', '', list1[i])
    list.append(match)
    i=i+1
return list

def linksappend(prefix,query):
i=0
list=[]
for i,item in enumerate(query):
    list.append(prefix + query[i])
    i=i+1
return list

#search functions

def searchwinecom(search):
prefix="http://www.wines.com"
url='http://www.wine.com/v6/search'
payload={'term':search}
r=requests.get(url,params=payload)
soup=BeautifulSoup(r.content,'html.parser')
wines=[a.find('a',class_="listProductName") for a in soup.find_all("div",class_='productName')]
prices=soup.find_all("div",class_="productPrice")
winelinks1=getlinks(wines)
winelinks=linksappend(prefix,winelinks1)
winename=filteryear(wines)
vintage=getyear(wines)
price=getprice(prices)

#code for putting data into dictionary below
i=0
for i,item in enumerate(winename):
    wine_results[winename[i]]=[winename[i],vintage[i],price[i],winelinks[i]]
    i=i+1
return

def searchwinesearcher(search):
search.replace(" ", "+")
url='http://www.wine-searcher.com/find/'+ search + '/1/usa'
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
winelinks=[a.find('a') for a in soup.find_all('div',class_="seller-link-wrap")]
winelink=getlinks(winelinks)
winenames=soup.find_all('span', class_="offer_winename")
winename=filteryear(winenames)
prices=soup.find_all('span',class_="offer_price boldtxt")
price=getprice(prices)
vintage=getyear(winenames)

#code for putting data into dictionary below
i=0
for i,item in enumerate(winename):
    wine_results[winename[i]]=[winename[i],vintage[i],price[i],winelink[i]]
    i=i+1
return

def searchcorkery(search):
prefix="http://www.corkerywine.com"
payload={'type':'product','q':search}
url='http://www.corkerywine.com/search'
r=requests.get(url, headers=hdr,params=payload)
soup=BeautifulSoup(r.content,'html.parser')
wines=[a.find('a') for a in soup.find_all("h2",class_='product-item__title')]
prices=soup.find_all("span",class_="product-item__price")
winelinks1=getlinks(wines)
winelinks=linksappend(prefix,winelinks1)
winename=filteryear(wines)
vintage=getyear(wines)
price1=getprice(prices)

#code for putting data into dictionary below
i=0
for i,item in enumerate(winename):
    wine_results[winename[i]]=[winename[i],vintage[i],price1[i],winelinks[i]] #change price1 to price after implementing error check
    i=i+1
return

def searchgetwineonline(search):
payload={'request':'SEARCH','search':search}
url='http://www.getwineonline.com/main.asp'
r=requests.get(url, headers=hdr,params=payload)
soup=BeautifulSoup(r.content,'html.parser')
vintages=soup.find_all("span",class_='vintage')
wines=soup.find_all("a",class_="producttitle")
prices=soup.find_all("span",class_="RegularPrice")
winelinks=getlinks(wines)
winename=gettext(wines)
vintage=gettext(vintages)
price=getprice(prices)

#code for putting data into dictionary below
i=0
for i,item in enumerate(winename):
    wine_results[winename[i]]=[winename[i],vintage[i],price[i],winelinks[i]]
    i=i+1
return 

def searchfunction(search):
searchwinecom(search)
searchwinesearcher(search)
searchcorkery(search)
searchgetwineonline(search)
wine_results2={}
for key,value in wine_results.items():
    wine_results2[key]=wine(wine_results[key][0], wine_results[key][1], wine_results[key][2],wine_results[key][3])
return wine_results2

我错过了什么?

0 个答案:

没有答案
相关问题