Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将从Chrome复制的css选择器路径转换为beautifulsoup obejct?_Python_Html_Beautifulsoup_Css Selectors - Fatal编程技术网

Python 如何将从Chrome复制的css选择器路径转换为beautifulsoup obejct?

Python 如何将从Chrome复制的css选择器路径转换为beautifulsoup obejct?,python,html,beautifulsoup,css-selectors,Python,Html,Beautifulsoup,Css Selectors,我一直在尝试创建一个函数,当从chromedev工具复制的css选择器路径给定时,该函数将返回beautifulsoup对象 当我从Chrome复制一个元素的css选择器路径时,它是这样的: body > table > tbody > tr:nth-child(2) > td.ColumnText2 如果我想让它成为一个合法的beautifulsoup对象,我会像 html = urlopen("https://someurl.com") bs = Beautifu

我一直在尝试创建一个函数,当从chromedev工具复制的css选择器路径给定时,该函数将返回beautifulsoup对象

当我从Chrome复制一个元素的css选择器路径时,它是这样的:

body > table > tbody > tr:nth-child(2) > td.ColumnText2
如果我想让它成为一个合法的beautifulsoup对象,我会像

html = urlopen("https://someurl.com")

bs = BeautifulSoup(html, 'html.parser')

bs.body.find_all('table')[3].find_all('tbody')[0].find_all('tr')[2].find_all('td', {'class': 'ColumnText2'})

但这真的很笨拙,如果我必须手动操作,那么创建函数就没有意义了。是否有某种内置解析器可以立即将此css选择器路径转换为beautifulsoup对象?

**请尝试在beautifulsoup中选择**

html = urlopen("https://someurl.com")

bs = BeautifulSoup(html, 'html.parser')

name_tags=bs .select("body > table > tbody > tr:nth-child(2) > td.ColumnText2")

还要注意的是,Chrome将在表中插入
tbody
元素,即使源代码中没有该元素。尝试从您的选择器中删除
>tbody

也许可以研究允许css搜索的方法?@politicalscientist哇,它马上就起作用了。我甚至什么都不用做。也许我应该更彻底地搜索一下。这解决了我的问题,谢谢。如果你回答问题而不是评论,我会确保选择答案。太好了
。select对我来说是一个游戏规则改变者:)真的。在我最终发现这一点之前,这让我过去很痛苦。谢谢你的意见。