Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

python - Find (Beautiful Soup) returns None in case the tag is only one

Could anyone help me please with understanding of this phenomenon? 'find' returns None in case the tag is only one. (I have already tried 'lxml', but it did not help). It is the part of what I am doing and receiving in my IDE:

>>> driver = webdriver.Chrome("D:\Text documents\for work\English project\chromedriver")
>>> driver.get('https://www.interactive-english.ru/uprazhneniya/408-conditionals-exercise/')
>>> content = driver.page_source
>>> soup = BeautifulSoup(content, 'lxml')
>>> FullList = soup.findAll(['ol', 'h4', 'h5']);
>>> htmlF = FullList[1]

>>> htmlF

<ol>
    <li>
        Perhaps one day a cat will follow you home.<br />
        What would you do...
    </li>
    <li>
        Perhaps one day somebody will ask you to sing your favourite song.<br />
        What would you do...
    </li>
    <li>
        Perhaps one day you will find a hidden treasure.<br />
        What would you do...
    </li>
    <li>
        Perhaps one day somebody will throw an egg at you.<br />
        What would you do...
    </li>
    <li>
        Perhaps one day your car will be stolen.<br />
        What would you do...
    </li>
</ol>
question from:https://stackoverflow.com/questions/66054295/find-beautiful-soup-returns-none-in-case-the-tag-is-only-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you did

htmlF = FullList[1]

The top level retrieved is the ol tag. There are no nested ol within it which is why when you call .find('ol') on it you get None.

You can check with:

print(htmlF.name)  
>> 'ol'

You can do

htmlF.find('li') 

for example to get first child as li are children within the parent ol you set htmlF to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...