bLAZ
bLAZ

Reputation: 1751

CSS selection in BeautifulSoup

I have a page which contains:

    <td class="tablec" width="50%">
        <div class="atomic"><strong>Date:</strong> Tuesday, October 31, 2017 At 08:00</div>
        <div class="atomic"><strong>Duration:</strong> 1 day</div>
    </td>

Information that I need to retrieve is that 1 day

Anyway, I do:

soup_cal = BeautifulSoup(page_cal.content, 'html.parser')
selected = soup_cal.select("td.tablec > div.atomic")
for lines in selected:
    print(lines.get_text())

but it is empty. Could you give some hint how to get this?

Upvotes: 0

Views: 51

Answers (1)

cs95
cs95

Reputation: 402483

If you're using bs4, the right way to extract text from a tag is using the .text/.content/.string properties.

for line in soup.select("td.tablec > div.atomic"):
     print(line.text)

Date: Tuesday, October 31, 2017 At 08:00
Duration: 1 day

If you only want the output from the last line, you'd use:

dur = soup.select("td.tablec > div.atomic")[-1].text.split(None, 1)[-1]

print(dur)
1 day

Upvotes: 1

Related Questions