如何将从Chrome复制的CSS选择器路径转换为beautifulsoup对象?

时间:2019-09-27 14:51:14

标签: python html beautifulsoup css-selectors

我一直在尝试提供一个从Chrome开发者工具复制的css选择器路径时返回beautifulsoup对象的函数。

当我从Chrome复制元素的CSS选择器路径时,它会像这样:

update drawings d
    set required_asbuilt = exists (select 1
                                   from revisions r
                                   where r.drawing_id = d.drawing_id and
                                         r.eb_suitability_status = 'obsolete'
                                  );

如果我希望它成为合法的beautifulsoup对象,我会像这样手动对其进行解析

body > table > tbody > tr:nth-child(2) > td.ColumnText2

但是,这确实很笨拙,如果我必须手动进行操作,那么编写函数毫无意义。是否有某种内置的解析器可以立即将此css选择器路径转换为beautifulsoup对象?

2 个答案:

答案 0 :(得分:1)

**,请尝试在Beautifulsoup中选择**

f, axes = plt.subplots(3,2) # Divide the plot into 3 rows, 2 columns
# Draw the plot in first row second column
sns.lineplot(xData, yData, data=dataSource, ax=axes[0][1]) 

答案 1 :(得分:1)

另请注意,即使源没有,Chrome 也会在表格中插入 tbody 元素。尝试从选择器中删除 > tbody