在Python中使用BeautifulSoup获取直接父标签 [英] Get immediate parent tag with BeautifulSoup in Python

查看:535
本文介绍了在Python中使用BeautifulSoup获取直接父标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了这个问题,但是还没有找到解决这个问题的实际方法.我正在将BeautifulSoup与Python结合使用,我想做的是从页面中获取所有图像标签,循环浏览每个图像标签,并检查每个图像标签的直接父级是否是锚标签.

I've researched this question but haven't seen an actual solution to solving this. I'm using BeautifulSoup with Python and what I'm looking to do is get all image tags from a page, loop through each and check each to see if it's immediate parent is an anchor tag.

这是一些伪代码:

html = BeautifulSoup(responseHtml)

for image in html.findAll('img'):
    if (image.parent.name == 'a'):
         image.hasParent = image.parent.link

对此有何想法?

推荐答案

您需要检查 name :

You need to check parent's name:

for img in soup.find_all('img'):
    if img.parent.name == 'a':
        print "Parent is a link"

演示:

>>> from bs4 import BeautifulSoup
>>> 
>>> data = """
... <body>
...     <a href="google.com"><img src="image.png"/></a>
... </body>
... """
>>> soup = BeautifulSoup(data)
>>> img = soup.img
>>> 
>>> img.parent.name
a


您还可以使用a父级的img标签. rel ="noreferrer"> CSS选择器:


You can also retrieve the img tags that have a direct a parent using a CSS selector:

soup.select('a > img')

这篇关于在Python中使用BeautifulSoup获取直接父标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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