I am building a site that allows users to submit links. I want to use BeautifulSoup (or something else if it works better for the task) to pull the title tag from the submitted URL and display it on a page that lists what links a user has submitted.
I tried the second answer on this page: How can I retrieve the page title of a webpage using Python?
It works in the shell, but I'm not sure where/how to use the soup.title.string to be able to populate the model.
If you need code examples of what I've tried so far, just ask. I didn't post it since I've tried running a few different things through.
Code for view:
def page_save(request):
if request.method == 'POST':
form = PageSaveForm(request.POST)
if form.is_valid():
# Create or get link.
link, dummy = Link.objects.get_or_create(
url=form.cleaned_data['url']
)
# Create or get bookmark.
page, created = Page.objects.get_or_create(
user=request.user,
link=link
soup=BeautifulSoup.BeautifulSoup(urllib.urlopen(link))
title=soup.title.string
The spacing is off, obviously.
I would be happy if users could save just the link and then I have the template pull the title from each link and display it to the end user.
Or even what SO does in the preview when I paste a URL.