I have this code in my views.py:
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from bs4 import BeautifulSoup
import urllib
def extract_links(request):
starting_link = urllib.urlopen("http://www.finalyearondesk.com")
soup = BeautifulSoup(starting_link)
all_links = soup.findAll('a', href = True)
return render_to_response('extracted_links.html',{'all_links': all_links })
In this I am usign BeautifulSoup.
And i am writing this code in the template file: extracted_links.html:
{% for final_links in all_links %}
{{ final_links['href'] }} # {{ final_links.href }} did not print anything
{% endfor %}
But the problem is it shows an error:
Could not parse the remainder: '['href']' from 'final_links['href']'
Any suggestion how to solve this? If I use this function on a simple python file, it just works fine but not on django template
Try:
I arrived at that from the following session:
If your version of BeautifulSoup has other attributes you can find them similarly.
You might have to extract them in the view though, and not in the template, e.g.:
before your return statement (or whichever attribute you need to access in your version of BeautifulSoup).