1

Currently using the below to scrape a local web page for flood information but because the top two rows have gone to 'danger' the class has slightly changed. It's not "row border py-2 bg-success" anymore it's "row border py-2 bg-danger" and there's probably a "row border py-2 bg-warning" as well to take into account as an edge case. How can I please look for "^row border py-2 bg-"? I was hoping ^ would work as begins with but it doesn't seem to! Thanks

import requests
from bs4 import BeautifulSoup

url = "http://nosearmy.com/isitflooded/"

r = requests.get(url)

soup = BeautifulSoup(r.content, "lxml")

g_data = soup.find_all("div", {"class": "row border py-2 bg-success"})


print g_data[0].text
print g_data[1].text
print g_data[2].text
print g_data[3].text
print g_data[4].text
print g_data[5].text
daneee
  • 153
  • 8
  • 1
    Does this answer your question? [How to find all divs who's class starts with a string in BeautifulSoup?](https://stackoverflow.com/questions/35465182/how-to-find-all-divs-whos-class-starts-with-a-string-in-beautifulsoup) – Sushil Nov 05 '20 at 12:36
  • Thanks for accepting my ans as the best ans. Can u also upvote my ans? Thanks! – Sushil Nov 05 '20 at 12:56

1 Answers1

3

Just use a simple lambda function:

g_data = soup.find_all("div", class_ = lambda x: x and x.startswith('row border py-2 bg-'))

Or you can also use regex:

import re 
g_data = soup.find_all("div", class_ = re.compile('^row border py-2 bg-'))
Sushil
  • 5,440
  • 1
  • 8
  • 26