Reputation: 13
I have the following code:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
url = 'https://www.drikpanchang.com/?geoname-id=5907364'
#Open web page
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
#Parse html
page_soup = soup(page_html, 'html.parser')
keys = page_soup.findAll('span', {'class': 'dpDainikPanchangKey'})
values = page_soup.findAll('span', {'class': 'dpDainikPanchangValue'})
print(keys[5])
print(values[5])
The output of the code is:
<span class="dpDainikPanchangKey">Karana</span>
<span class="dpDainikPanchangValue">Bava <span class="dpOffColor">upto</span> 08:01 <span class="dpTimeStamp">AM</span></span>
How do I access 'Karana' in keys[5] and 'Bava' in values[5] and assign them to variables?
Upvotes: 1
Views: 750
Reputation: 4779
You can do like this.
To get the value from keys[5]
, just do
keys_5 = keys[5].text.strip()
To get the desired value from values[5]
,
Use stripped_strings
and get the first item.
val_5 = next(values[5].stripped_strings)
You can also use .contents
this way
val_5 = values[5].contents[0]
keys_5 = keys[5].text.strip()
val_5 = next(values[5].stripped_strings)
print(f'keys_5: {keys_5}')
print(f'val_5: {val_5}')
keys_5: Karana
val_5: Kaulava
Upvotes: 0
Reputation: 359
Use the var_1 = keys[5].get_text()
or var_1 = keys[5].text
. More about the get_text() method can be found on the bs4 documentation page.
For values
, use something like var_2 = values[5].text.split()[0]
to get 'Bava'.
To explain, .text
or .get_text()
retrieves the text attribute of the tags you parsed.
There may be a more elegant and bespoke solution for 'Bava', but this will get the job done.
Upvotes: 1