-1

I am attempting to learn about web scraping. I am getting the position of "Basic EPS" from the webpage using a generator found How to get item's position in a list?. However I am receiving this error message:

gen = (i for i,x in enumerate(div) if x == eps)
for i in gen: print(i)
--->  print(i)
NameError: name 'i' is not defined

My code is:

import requests
from bs4 import BeautifulSoup
import pprint

PAGE ="https://au.finance.yahoo.com/quote/QAN.AX/financials?p=QAN.AX"
result = requests.get(PAGE)

type(result)
source = result.text
soup = BeautifulSoup(source, 'html.parser')
div = soup.find_all('div', class_='rw-expnded')
#pprint.pprint(div)
eps = soup.find('div', title='Basic EPS')
#pprint.pprint(eps)
gen = (i for i,x in enumerate(div) if x == eps)
for i in gen: print(i)
print(i)

I am a bit confused by this as I didn't think I needed to define "i". I then am planning to use "i" in an arithmetic calculation to find a position of another section so the expected result should be 10 as an int.

Does anyone have any tips?

EDIT: To add the outputs for div and eps. print(div) has huge output that exceeds maximum characters but contains:

<div class="D(ib) Va(m) Ell Mt(-3px) W(215px)--mv2 W(200px) undefined" data-reactid="286" title="Basic EPS"><span class="Va(m)" data-reactid="287">Basic EPS</span></div>

print(eps)

<div class="D(ib) Va(m) Ell Mt(-3px) W(215px)--mv2 W(200px) undefined" data-reactid="286" title="Basic EPS"><span class="Va(m)" data-reactid="287">Basic EPS</span></div>
Aidan01
  • 25
  • 1
  • 7
  • It's because your loop is never executed. Make sure that `gen` is not empty. – Asocia Jun 10 '20 at 10:58
  • Does that mean making sure that div and eps have values? – Aidan01 Jun 10 '20 at 11:05
  • You need to be sure that `div` is not empty and `x == eps` will be `True` at least once. So there will be value(s) in the `gen` to iterate over. – Asocia Jun 10 '20 at 11:08
  • Thought that might have been the case. Have tried that, there is exactly one instance of `x == eps` within `div`. At position 10 in the list. – Aidan01 Jun 10 '20 at 11:10
  • So you should see it printed into the screen, before the exception is raised. But when I run your code, it doesn't print any output, so `gen` is empty. – Asocia Jun 10 '20 at 11:12
  • Thanks for clearing it up, although `eps` is appearing in `div`, it is part of a much bigger amount of code within the position of the list – Aidan01 Jun 10 '20 at 11:30

1 Answers1

0

Added inline comments

# below i, x are in inside a list-comprehension, so they are not avail to print in the main program
gen = (i for i,x in enumerate(div) if x == eps) 

# If any item is present in gen, only then `i` gets initialized and printed
for i in gen: print(i)

# following can give you a NameError if `i` did not get initialized before this step.
print(i)

It is not suggestible to print(i) like above but if you want to print last item of gen, then do the following

if gen:
    print(i)

or a better option

if gen:
    print(gen[-1])
sam
  • 1,819
  • 1
  • 18
  • 30