5

Python爬虫编程思想(54):使用Beautiful Soup选择兄弟节点

 2 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/120796952
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python爬虫编程思想(54):使用Beautiful Soup选择兄弟节点

        除了子节点和父节点外,还有同级节点,也称为兄弟节点。可以通过next_sibling属性获得当前节点的下一个兄弟节点,通过previous_sibling属性获得当前节点的上一个兄弟节点。通过next_siblings属性获得当前节点后面所有的兄弟节点(返回一个可迭代对象),通过previous_siblings属性可以获得当前节点前面所有的兄弟节点(返回一个可迭代对象)。

        这里要注意,如果两个节点之间有换行符或其他文本,那么这些属性也同样会返回这些文本节点,节点之间的文本将作为一个文本节点处理。文本节点是bs4.element.NavigableString类的实例,而普通节点是bs4.element.Tag类的实例。

现在以下面的HTML代码为例,说明文本节点和普通节点的区别。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK