Get immediate parent tag with BeautifulSoup in Python

会有一股神秘感。 提交于 2020-03-13 04:18:12

问题


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.

Here's some pseudo code:

html = BeautifulSoup(responseHtml)

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

Any ideas on this?


回答1:


You need to check parent's name:

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

Demo:

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

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

soup.select('a > img')


来源:https://stackoverflow.com/questions/27874579/get-immediate-parent-tag-with-beautifulsoup-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!