0

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.

Community
  • 1
  • 1

1 Answers1

1

EDIT: Misunderstood the question...

It would be helpful to see your model. The code to create the page looks suspect. Is soup a field in your model? Perhaps this is what you are trying to do?

page, created = Page.objects.get_or_create(
    user=request.user,
    link=link,
    title=BeautifulSoup.BeautifulSoup(urllib.urlopen(link)).title.string
  )

Some bigger questions for you. What happens when your user submits a malicious or invalid link? What happens when the url times out? Are there other times beyond this view where you might want to take the link from your page instance and create a title?

bpoetz
  • 129
  • 1
  • 4