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>