izzyp
izzyp

Reputation: 173

Find all occurrences of string in html file

#code above, including import requests and import BeautifulSoup
page = requests.get(url) #gets the url I want to scrape
html = BeautifulSoup(page.text, "html.parser")
[h.extract() for h in html('script')]

tracklist = html.find("h3", class_="chart_row-content-title").get_text()

I'm aiming to scrape song titles via html websites through this method

This gets all the text that I'm looking for within one heading, however there are multiple places in the file in which I want the same corresponding text. Any ideas on how to get all occurrences of this?

Any suggestions would be greatly appreciated!

Upvotes: 0

Views: 542

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you can use find_all(), like:

for el in html.find_all("h3", class_="chart_row-content-title"):
    tracklist = el.get_text()

or you could also extract texts as:

allTexts = [ele.get_text() for ele in html.select('h3.chart_row-content-title')]

Upvotes: 3

Related Questions