How to get <text> tag from an html document using beautiful soup

Viewed 193

How can i get the <text> tag from an html document using beautiful soup for Abbot lab 10k filing

I want to extract tag names of all children of <text></text> tag using below code

from bs4 import BeautifulSoup
import urllib.request
url ='https://www.sec.gov/Archives/edgar/data/1800/000104746919000624/a2237733z10-k.htm'
htmlpage = urllib.request.urlopen(url)
soup = BeautifulSoup(htmlpage, "html.parser")
all_text = soup.find('text')
all_tags = all_text.contents
all_tags = [x.name for x in all_tags if x.name is not None]
print(all_tags)

but some how the output i am getting for the above code is ['html'].

Expected output:
['p','p','p','p','p','p','div','div','font','font', etc......]

2 Answers

You can use CSS selector (for printing all children of tag text):

for child in all_text.select('text *'):
    print(child.name, end=' ')

Prints:

br p font font b p font b br p font b div div ...

EDIT: For printing only direct children of tag text, you can use:

from bs4 import BeautifulSoup
import requests

url ='https://www.sec.gov/Archives/edgar/data/1800/000104746919000624/a2237733z10-k.htm'

htmlpage = requests.get(url)
soup = BeautifulSoup(htmlpage.text, "lxml")

for child in soup.select('text > *'):
    print(child.name, end=' ')

Replace your code :

all_tags = all_text.contents
all_tags = [x.name for x in all_tags if x.name is not None]
print(all_tags)

TO:

all_tags = [x.name for x in all_text.findChildren() if x.name is not None]
print(all_tags)

findChildren() more details

Related