這一行: 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}
uj5u.com熱心網友回復:
因此,要在一個范圍內回圈,您可以在范圍內執行 for _ (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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/460069.html
上一篇:從動態網頁中抓取不可互動的表格
