Issue with finding parent of a particular tag in html using python

守給你的承諾、 提交于 2019-12-08 09:37:40

问题


I am trying to fetch parent element of a particular tag using below mentioned code:

# -*- coding: cp1252 -*-
import csv
import urllib2
import sys
import time
from bs4 import BeautifulSoup
from itertools import islice
page1= urllib2.urlopen('http://www.sfr.fr/mobile/telephones?vue=000029&tgp=toutes-les-offres&typesmartphone=se-android&typesmartphone=se-apple&typesmartphone=se-bada&typesmartphone=se-rim-blackberry&typesmartphone=se-windows&p=0').read()
soup1 = BeautifulSoup(page1)
price_parent = soup1.findParents('div')
print price_parent

Problem: Output which I am getting after running this code returns Null array [], if I use findParent instead of Parents then also it returns None value.

My actual problem is similar to this BeautifulSoup - findAll not within certain tag

To solve my actual problem I need to get parents of elements for which I am getting None value as mentioned above.

Please help me in solving this issue and pardon my ignorance as I am new to programming.


回答1:


.findParents() does not do what you think it does. It finds the parents of the current element that match the search. You are trying to find the parents of a page element, which is already the top-level element.

If you had a structure like this:

<html>
    <body>
        <div class="foo">
            <span id="bar">Some text</span>
        </div>
    </body>
</html>

where soup is a BeautifulSoup variable for the whole structure, you can find the span with:

spanelement = soup.find('span', id='bar')

and then calling .findParent('div') will return a result, namely the <div class="foo"> element.

So, calling .findParents() on a top-level element will always return an empty result, there are no parents. Call it on something that does have a parent element instead.



来源:https://stackoverflow.com/questions/14135190/issue-with-finding-parent-of-a-particular-tag-in-html-using-python

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