###前提・実現したいこと
BeutifulSoupを使用したクローリングで数値を取得
Djangoのテンプレートに数値を表示する
###発生している問題・エラーメッセージ
typeerror : context must be a dict rather than NavigableString.
###該当のソースコード
view
python
1from django.shortcuts import render 2from django.contrib.auth.decorators import login_required 3from bs4 import BeautifulSoup 4import urllib.request 5 6 7def index(request): 8 return render(request, 'scrapings/index.html') 9 10 11def scraping(request): 12 html = urllib.request.urlopen("url") 13 soup = BeautifulSoup(html, "html.parser") 14 span = soup.find_all("span") 15 16 xxx = "" 17 for tag in span: 18 try: 19 string_ = tag.get("class").pop(0) 20 if string_ in "class": 21 xxx = tag.string 22 break 23 except: 24 pass 25 return render(request,'scrapings/scraping.html',xxx)
template
python
1{% if xxx %} 2 3<ul> 4 {% for item in xxx %} 5 <li>{{ item }}</li> 6 {% endfor %} 7</ul> 8{% endif %}
###コメント
typeerrorということでおそらく単純な問題だとは思うのですが、うまく解決できておりません。
申し訳ございませんがご教授お願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/21 01:42