Get immediate parent tag with BeautifulSoup in Python

I investigated this question, but did not see a real solution to this. I am using BeautifulSoup with Python, and what I want to do is get all the image tags from the page, skip them and check each one to see if it is the immediate parent of the anchor tag.

Here is 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?

+6
source share
1 answer

You need to check parent 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 get img tags that have a direct parent a using the CSS selector :

 soup.select('a > img') 
+5
source

Source: https://habr.com/ru/post/980798/


All Articles