this line: share_details1 = soup.find('a', href="../Industry/Industry_Data.php?s=100") I want to also find instead of 100 or 200 or 300 0r 400 etc until 1300
example ../Industry/Industry_Data.php?s=200
def get_sector(ticker):
soup = get_soup(LSE ticker)
try:
share_details1 = soup.find('a', href="../Industry/Industry_Data.php?s=100")
messy = share_details1.find("span")
messy.decompose()
sector = share_details1.text.strip()
except:
print('No sector information availible for ', ticker)
return {'ticker': ticker, 'sector': ''}
print(ticker, sector)
return {'ticker': ticker, 'sector': sector}
CodePudding user response:
So to loop on a range you can do a for _ in range(start, stop, step):
Assuming you want it all wrapped in a function and you can accept the output to be an array of dictionaries:
def get_sector(ticker):
soup = get_soup(LSE ticker)
result = []
for s in range(100, 1400, 100): #starting here 's' is your changing value
try:
share_details1 = soup.find('a', href=f'../Industry/Industry_Data.php?s={s}') #plug in here
messy = share_details1.find("span")
messy.decompose()
sector = share_details1.text.strip()
except:
print('No sector information availible for ', ticker)
return {'ticker': ticker, 'sector': ''}
print(ticker, sector)
result.append({'ticker': ticker, 'sector': sector})
return result