Home > Enterprise >  how to get text from b tag inside a span class bs4
how to get text from b tag inside a span class bs4

Time:03-08

my code till now

from bs4 import BeautifulSoup as bs
import requests,time

url = "https://forecast.weather.gov/MapClick.php?lat=37.8615&lon=-87.0611"
response = requests.get(url)
soup = bs(response.content,'lxml')

data2 = soup.find('span',{"class":"smallTxt"})
print(data2)

the output is BRIDESHEAD REVISITED by Evelyn Waugh

CodePudding user response:

Using your code i cant get BRIDESHEAD REVISITED

You can try doing something like this, it's not robust, but it might work.

I define a dictionary and for each key I get its value.

To simplify the resolution I remove the colon from each key.

The format does not allow to iterate and take each key-value in the same row, so I define the key and its value (empty) and in the next round I insert it

result = {}
for i,v in enumerate(data2):
    if i % 2 == 0:
        key = v.get_text().split(':')[0]
        result[key] = ''
    else:
        result[key] = v

output:

{'Lat': '37.74°N', 'Lon': '87.17°W', 'Elev': '407ft.'}
  • Related