Python Django如何使用模板标记拆分字符串
您好,我想知道如何拆分字典值的字符串 这是我的爬虫,它返回字典数据Python Django如何使用模板标记拆分字符串,python,django,Python,Django,您好,我想知道如何拆分字典值的字符串 这是我的爬虫,它返回字典数据 data = { {0:'http://..., product name, product price'}, {1:'http://...2, product name2, product price2'}, {N:'http://...2, product name2, product price n'} } 我想用逗号分割这些数据 像 在Django 这是我的爬虫 import requests
data = {
{0:'http://..., product name, product price'},
{1:'http://...2, product name2, product price2'},
{N:'http://...2, product name2, product price n'}
}
我想用逗号分割这些数据
像
在Django
这是我的爬虫
import requests
from urllib import parse
from bs4 import BeautifulSoup
def spider(item_name):
url_item_name = parse.quote(item_name.encode('euc-kr'))
url = 'http://search.11st.co.kr/SearchPrdAction.tmall?method=getTotalSearchSeller&isGnb=Y&prdType=&category=&cmd=&pageSize=&lCtgrNo=&mCtgrNo=&sCtgrNo=&dCtgrNo=&fromACK=recent&semanticFromGNB=&gnbTag=TO&schFrom=&schFrom=&ID=&ctgrNo=&srCtgrNo=&keyword=&adUrl=&adKwdTrcNo=&adPrdNo=&targetTab=T&kwd=' + url_item_name
resp = requests.get(url)
resp.raise_for_status()
resp.encoding='euc-kr'
plain_text = resp.text
soup = BeautifulSoup(plain_text, 'lxml')
mytag = soup.find_all(True, {"class": ["sale_price", "list_info"]})
#for link in soup.select('div.list_info p.info_tit a') :
data = {}
count = -1;
for link in mytag:
if(link.find('a')):
count+=1
href = link.find('a').get('href')
product_name = link.find('a').string
data[count] = str(href) + ", " + str(product_name)
else:
product_price = link.string
if(product_price):
data[count] = data[count] +", " + str(product_price)
for value in data.values():
print(value)
resp.close()
return data
这是我的观点
def post_shop_list(request):
posts = spider("product name")
return render(request, 'blog/post_list.html',{'posts' : posts})
这是我的post_list.html
{% for key, value in posts.items %}
<div>
<td>{{key}}</td>
<p>product name :{{value}}</p>
<h1><a href=href> </a></h1>
<p>{{ product_price|linebreaksbr}}</p>
</div>
{% endfor %}
{%用于键,posts.items%中的值]
{{key}}
产品名称:{{value}}
{{产品价格{linebreaksbr}}
{%endfor%}
谢谢你 我建议不要在模板中做这类事情。最终,它们中嵌入了一半的视图逻辑。 在你看来,我建议你做这样的事情,或者更好的是你的爬虫: 最后,您得到了一个不错的dict数组,将其交给模板,然后在模板上迭代并读取元素字段
{% for item in products %}
<td>{{ item.key }}</td>
<p>product name :{{ item.name}}</p>
<h1><a href={{ item.href }}> </a></h1>
<p>{{ item.price }}</p>
{% endfor %}
{%用于产品中的项目%}
{{item.key}
产品名称:{{item.name}
{{item.price}}
{%endfor%}
或者,按照所述创建自定义模板标记
在Django模板中,您可以像这样使用它
# assuming value = "url, product_name, product_price"
# and you have always these three comma separated items in sequence
{% for key, value in posts.items %}
<tr>
{% with value|split:"," as details %}
{% for p in details %}
<td>{{ p }}</td>
{% endfor %}
{% endwith %}
</tr>
{% endfor %}
然后将此标记加载到html文件的顶部,您要在其中使用split
filter
{% load filter_tags %}
感谢您的友好回答,我只是尝试使用此解决方案,但它显示了第一个产品数据..我尝试使用此解决方案,但出现了类似django.template.exceptions.TemplateSyntaxError的错误:无效筛选器:“拆分”我创建了一个templatetags目录,创建了init.py,创建filter.py,我从django import template register=template.Library()@register.filter(name='split')def split(value,key):返回value.split(key)register.filter('split',split)检查我的更新部分,您错过了一些东西。
from django import template
register = template.Library()
@register.filter(name='split')
def split(value, key):
"""
Returns the value turned into a list.
"""
return value.split(key)
# assuming value = "url, product_name, product_price"
# and you have always these three comma separated items in sequence
{% for key, value in posts.items %}
<tr>
{% with value|split:"," as details %}
{% for p in details %}
<td>{{ p }}</td>
{% endfor %}
{% endwith %}
</tr>
{% endfor %}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'loaders': [
...
],
'context_processors': [
...
],
'libraries':{
# make your file entry here.
'filter_tags': 'app.templatetags.filter',
}
},
},
]
{% load filter_tags %}