【发布时间】:2020-05-31 07:52:42
【问题描述】:
threads = soup.find_all('tr',id=re.compile('^eventRowId.+'))
for thread in threads:
t = datetime.datetime.strptime(thread['event_timestamp'],'%Y-%m-%d %H:%M:%S')
event_times.append(datetime.datetime.strftime(t,'%d-%m'))
到目前为止,上面的脚本运行良好。
for thread in threads:
performance = thread.find_all('td',title=re.compile('^[IBW].+'))
print(performance['title'])
在尝试添加这三行之后,再做一次“更深入的搜索”,就会发生错误。 我再次进行了搜索,因为我想提取“tr”索引之后的嵌套“td”
TypeError:列表索引必须是整数或切片,而不是 str
不知何故,性能变量似乎不再是字典。
【问题讨论】:
-
请分享整个错误信息,以及minimal reproducible example。这个错误对我来说似乎不言自明,我建议阅读ericlippert.com/2014/03/05/how-to-debug-small-programs。
-
我应该写 performance[0]['title'] 而不是 performance['title'] find_all 生成一个列表而不是字典。
标签: python regex beautifulsoup