试图从ulsi网站解析ulsa表。(我无法共享网站源代码,因为它是限制使用的。)
我试图提取数据,只有当它有以下两个标记和这些特定的类。在
td, width=40%
tr, valign=top
我这样做的原因是提取同时具有这些标记和类的数据。在
我发现了一些关于使用多个标记here的讨论,但这篇文章只讨论了标记,而不是类。但是,我确实尝试用同样的逻辑来扩展代码,但是我认为我得到的不是我想要的:
^{pr2}$
总之,我的问题是如何使用多个标记,每个标记在find_all中都有一个特定的类,以便结果“and”两个标记。在
Tags:
假设bsObj是你美丽的汤对象 尝试:
希望这有帮助。在
您可以将
re.compile
对象与soup.find_all
一起使用:输出:
^{pr2}$通过提供}标记的任何实例,该标记包含}的内联
re.compile
对象来指定所需的标记和style
值,find_all
将返回tr
或{width:40%
或{style
属性。在此方法可以通过提供多个属性值来推断元素:
输出:
编辑2:简单递归解决方案:
输出:
递归函数使您能够为某些标记提供自己的字典所需的目标属性:此解决方案尝试将任何指定属性与传递给函数的
bs4
对象相匹配,如果发现匹配,则元素为yield
ed相关问题 更多 >
编程相关推荐