0

I'm trying to use beautiful soup 4 to parse html for a login page and get tokens from that page.

import requests 
from bs4 import BeautifulSoup

session = requests.Session()

login_page_html = BeautifulSoup(session.get('https://url.com',     verify=False).text)
lsd = value_from_name('something', login_page_html)


def value_from_name(name, soup):
    return soup.find(name=name)['value']

I got this to work in another program, but I'm not sure why it's not working here. I'm new to python, guessing it's because I'm not passing the paramaters correctly?

  • What is `login_page_html`? What does _not working_ mean? – kylieCatt Jul 30 '15 at 17:40
  • Or another duplicate: http://stackoverflow.com/questions/15119451/call-a-function-in-python-getting-function-is-not-defined. – alecxe Jul 30 '15 at 17:40
  • My bad, I accidentally had page_html = ... instead of login_page_html, it's been fixed in the OP now. @alecxe thanks for the link, thought I searched thoroughly for the answer and I thought in python it doesn't matter if you define the functions before or after where you call them. – Eric Miller Jul 30 '15 at 18:56

1 Answers1

1

You are using the function before defining it. Define the function first, before using it.

Example -

import requests 
from bs4 import BeautifulSoup


def value_from_name(name, soup):
    return soup.find(name=name)['value']

session = requests.Session()

page_html = BeautifulSoup(session.get('https://url.com', verify=False).text)
lsd = value_from_name('something', login_page_html)
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176