我想找到不同的标签 beautifulsoup python 函数
i want to find a different tags beautifulsoup python function
这一行:share_details1 = soup.find('a', href="../Industry/Industry_Data.php?s=100")
我还想找到而不是 100 或 200 或 300 0r 400 等直到 1300
例子../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}
因此,要在范围内循环,您可以执行 for _ in range(start, stop, step):
假设您希望将其全部包装在一个函数中,并且您可以接受输出为字典数组:
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
这一行:share_details1 = soup.find('a', href="../Industry/Industry_Data.php?s=100") 我还想找到而不是 100 或 200 或 300 0r 400 等直到 1300
例子../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}
因此,要在范围内循环,您可以执行 for _ in range(start, stop, step):
假设您希望将其全部包装在一个函数中,并且您可以接受输出为字典数组:
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