前提・実現したいこと
リストの中の対象の文字列以外を別の文字列に置き換える方法が知りたいです。
先ほど同様の質問を投稿し、実行した際には問題なく動いたのですが、現在エラーが出ました。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) <ipython-input-88-75d1a998f799> in <module> 13 data['結果'] = res 14 else: ---> 15 data['結果'] = '当選' 16 dat = {} 17 dat['結果'] = res if res else '当選' TypeError: list indices must be integers or slices, not str
該当のソースコード
python
1soup = BeautifulSoup(page_source, 'html.parser') 2 3a = [] 4 5spots = soup.find_all('div', attrs={'class': 'element-rhythm-extra-long anchor-pt'}) 6for spot in spots: 7 reslets = spot.find('div', attrs={'class': 'ttl-result-area'}) 8 spot.find('span', attrs={'class': 'horizontal-rhythm'}).extract() 9 spot.find('h3', attrs={'class': 'ttl-stock'}).extract() 10 spot.find('div', attrs={'class': 'ttl-stock-area'}).extract() 11 res = reslets.text.replace('\n', '') 12 if res == '落選': 13 data['結果'] = res 14 else: 15 data['結果'] = '当選' 16 dat = {} 17 dat['結果'] = res if res else '当選' 18 19 a.append(dat) 20
補足情報(FW/ツールのバージョンなど)
わかりにくいところが多々あるかと思います。細かい点でもご質問いただければなるべく早く返信いたします。
回答1件
あなたの回答
tips
プレビュー