问题描述
在BeautifulSoup中将CSS选择器与轴组合器一起使用时,我有些困惑.下面是简单的代码来说明我的意思:
I'm a bit confused by using CSS selectors with axis combinators in BeautifulSoup. Below is the simple code to illustrate what I mean:
from bs4 import BeautifulSoup as bs
import requests
response = requests.get('https://stackoverflow.com/questions/tagged/python')
soup = bs(response.text)
print(len(soup.select('#mainbar > div')))
返回6
个孩子...但是
print(len(soup.select('#mainbar>div')))
返回0
个孩子...
与'#mainbar ~ div'
(发现1个兄弟姐妹)和#mainbar~div'
(没有发现任何东西)相同
The same with '#mainbar ~ div'
(found 1 sibling) and #mainbar~div'
(found nothing)
在文档中,这些空格是可选的,但在实际上,对于相同的选择器,我使用BeautifulSoup获得了不同的输出(我认为)
From documentation those spaces are optional, but in fact I got different output with BeautifulSoup for the same selectors (as I thought)
是bs4
错误还是这种行为取决于CSS版本或其他?
So is it bs4
bug or this behavior depends on version of CSS or something else?
推荐答案
在此处确认为错误: https://bugs.launchpad.net/beautifulsoup/+bug/1717851
从CSS角度来看,选择器可以使用/不使用.
The selector, from a CSS perspective is fine with/without.
我会看看是否可以找到进一步的证据.
I will see if I can find further evidence.
报告错误的个人指出:
这篇关于CSS组合器周围的空格是否真的是可选的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!