CSS组合器周围的空格是否真的是可选的 [英] Are spaces around CSS combinators are really optional

查看:82
本文介绍了CSS组合器周围的空格是否真的是可选的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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?

推荐答案

在此处确认为错误:从CSS角度来看,选择器可以使用/不使用.

The selector, from a CSS perspective is fine with/without.

我会看看是否可以找到进一步的证据.

I will see if I can find further evidence.

报告错误的个人指出:

据我所知,问题在于由于代码仅在执行 shlex.split,它不会将div>span分开对待 实体是>两侧的空白.

The issue, as far as I see, is that since the code is only doing a shlex.split, it doesn't treat div, >, and span as separate entities is a space is left out on either side of >.

这篇关于CSS组合器周围的空格是否真的是可选的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆