【发布时间】:2016-11-15 14:18:52
【问题描述】:
首先,如果您看我的帖子,我要感谢您。我发现了很多关于如何使用 BS4 阅读下一个元素的帖子,但它涉及到关键字相关的问题。
这是我的问题:我尝试从 txt.files 中删除数据,而构建 HTML 的方式对于不同的变量有类似的环境。
例如,这里是我要提取的变量之一:
(不关注encode/decode部分)
number= bs.find_all('span', class_='grid_1 prefix_1 suffix_1 data')[0].get_text().encode('ascii', 'ignore').decode(
'ascii')
它工作得很好,但现在我要提取的下一个变量出现在 number 之后具有完全相同的 html 构建。所以当我跑步时
Local= bs.find_all('span', class_=''span', class_='grid_1 prefix_1 suffix_1 data')[0].get_text().encode('ascii', 'ignore').decode(
'ascii')
number= bs.find_all('span', class_='grid_1 prefix_1 suffix_1 data')[0].get_text().encode('ascii', 'ignore').decode(
'ascii')
它为我提供了两个变量的相同信息。据我所知,BS4 在他第一次遇到插入到 findall 中的元素时就停止了。
阅读 Beautiful Soup 文档后,我尝试使用 find_next 命令获取与第二个元素对应的数据。 当我跑步时:
Local= bs.find_all('span', class_='grid_1 prefix_1 suffix_1 data')[0].find_all_next().encode('ascii', 'ignore').decode(
'ascii')
我收到以下 Python 错误: AttributeError: 'ResultSet' 对象没有属性
当我尝试单独运行 find_next 命令时:
Local= bs.find_next('span', class_='grid_1 prefix_1 suffix_1 data')[0].encode('ascii', 'ignore').decode(
'ascii')
我收到以下 Python 错误: TypeError: 'NoneType' 对象没有属性 '__getitem__'
我的问题是“如何正确地将 find_next 命令应用于 find_all?”
【问题讨论】: