1

I want to extract all strings included in strong tags and save them in a file. But I cannot extract strong tags; it only returns empty array. Am I missing anything. Here is the code that I have used: Input:

>>>Import requests
>>>from bs4 import BeautifulSoup
>>>r=requests.get(' http://www.webmd.com/heart-disease/heart-disease-glossary#1')
>>>r.content
>>> soup=BeautifulSoup(r.content,"lxml")
>>>print(soup)
>>>soup.find_all('strong')

Output for the last statement:

[]

Thanks,

2
  • Are there any strong tags in the source? I followed your link and I didn't see anything inside a strong tag.
    – meisen99
    Sep 12, 2017 at 1:51
  • @meisen99 you are right, I needed to look for <b> instead of <strong>
    – NinaDev
    Sep 12, 2017 at 2:00

1 Answer 1

1

Actually, there isn't a strong tag in the soup.

When open the url in a browser, you can get two strong tags. But I don't think you need them.

<li xmlns="http://www.w3.org/1999/xhtml">Check the address for typing errors such as
<strong>ww</strong>.example.com instead of
<strong>www</strong>.example.com</li>

If you mean to get all the terms in bold, you should use .find_all('b').

[tag.text.strip().replace(':',"") for tag in soup.find_all('b')]

outputs:

['Ablation','Advance Directive (living will)','Aerobic Exercise', ...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.