DiegoT
DiegoT

Reputation: 29

Python: How can i get text from a tag like this in BeautiflSoup

I need to get the date and hour of this links : 'https://www.pagina12.com.ar/225378-murio-cacho-castana-simbolo-del-macho-porteno' or any in the site 'https://www.pagina12.com.ar/'.

the structure is this:

<div class="article-info"><div class="breadcrumb"><div class="suplement"><a href="https://www.pagina12.com.ar/suplementos/cultura-y-espectaculos/notas">Cultura y Espectáculos</a></div><div class="topic"></div></div><div class="time"><span datetime="2019-10-15" pubdate="pubdate">15 de octubre de 2019</span><span> · </span><span>Actualizado hace <span class="article-time" data-time="1571156914">3 hs</span></span></div></div>

and i did this:

cosa = requests.get('https://www.pagina12.com.ar/225378-murio-cacho-castana-simbolo-del-macho-porteno').text
parse = BeautifulSoup(cosa, 'html5lib')
info = parse.findAll('div', {'class':'article-info'})

then i try to get the text that says '3 Hs' and cant access to it and dont know how to do it. Anyone have an idea ?

Thanks!

Upvotes: 0

Views: 42

Answers (1)

QHarr
QHarr

Reputation: 84465

You could calculate from the data-time attribute

from bs4 import BeautifulSoup as bs
import requests, datetime
import dateutil.relativedelta

r = requests.get('https://www.pagina12.com.ar/225378-murio-cacho-castana-simbolo-del-macho-porteno')
soup = bs(r.content, 'lxml')
dt1 = datetime.datetime.fromtimestamp(float(soup.select_one('[data-time]')['data-time']))
dt2 = datetime.datetime.fromtimestamp(datetime.datetime.now().timestamp()) 
diff = dateutil.relativedelta.relativedelta(dt2, dt1)
print(diff.hours)

Upvotes: 1

Related Questions