gpt4 book ai didi

python - Django 如何使用模板标签拆分字符串

转载 作者:太空狗 更新时间:2023-10-30 02:15:52 27 4
gpt4 key购买 nike

你好,我想知道如何拆分字典值字符串

这是我的爬虫,它返回的字典数据看起来像

data = {
{0:'http://..., product name, product price'},
{1:'http://...2, product name2, product price2'},
{N:'http://...2, product name2, product price n'}
}

我想用逗号分割这些数据 喜欢,

for value in data.values():
href, product_name, product_price = str(value).split(",")

在 Django 中

这是我的crawler.py

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 %}

谢谢..!!

最佳答案

创建自定义 template filter

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)

在 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 %}

更新

您还必须在 libraries 关键字的 TEMPLATE 列表中输入您的标记文件。

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',
}
},
},
]

然后将此标记加载到您要使用 split 过滤器的 html 文件顶部

{% load filter_tags %}

关于python - Django 如何使用模板标签拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com