2

Python爬虫编程思想(53):使用Beautiful Soup选择父节点

 2 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/120721802
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.
neoserver,ios ssh client

Python爬虫编程思想(53):使用Beautiful Soup选择父节点

专栏收录该内容
53 篇文章 2 订阅 ¥29.90 ¥99.00

        如果要选取某个节点的直接父节点,需要使用parent属性,如果要选取某个节点的所有父节点,需要使用parents属性。parent属性返回当前节点的父节点的Tag对象,而parents属性会返回一个可迭代对象,通过for循环可以对该对象进行迭代,并获得当前节点所有的父节点对应的Tag对象。

        下面的例子通过parent属性获得a节点的直接父节点,然后使用parents属性获得a节点所有的父节点,并通过迭代,输出a节点所有父节点的标签名。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK