我知道如何借助 attrs 参数根据确切的 ID 进行过滤。
tables = pd.read_html(url, attrs={"id": "box-CHI-game-basic"})
我事先不知道确切的ID,但我知道它的结构。我可以使用正则表达式捕获 id:
re.search(".+-game-basic", "box-CHI-game-basic")
如果您只是添加正则表达式作为 attr 的值,这是行不通的。
read_html的match参数可以使用正则表达式,但它会遍历整个文本,我想将其缩小到id。
最佳答案
我认为您无法使用 pandas_html
做到这一点。
match
参数将匹配:
The set of tables containing text matching this regex or string
至于attrs
,您尝试的操作将不起作用,因为
attrs
is a dictionary of attributes that you can pass to use to identify the table in the HTML. These are not checked for validity before being passed to lxml or Beautiful Soup. However, these attributes must be valid HTML table attributes to work correctly
所以,我想你必须首先求助于 bs4
,例如:
soup.find_all("table", id=re.compile(".+-game-basic")
然后,将表传递给 pandas
进行进一步解析。
关于html - 基于正则表达式 attr 从 read_html 过滤表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74487351/