How to pick a value from a certain attribute with Beautifulsoup

Asked

Viewed 592 times

1

I’m trying to write a code that takes titles from the G1 news and prints them on the screen. The problem is that G1 uses unfinished text(...), ex:

<a class=(...) title="Marcelo Bretas acolhe pedido de transferência de Sérgio Cabral para presídio federal" href=(...)">Marcelo Bretas acolhe pedido de transferência de Sérgio Cabral...</a>

However the attribute "title" contains them complete. How to use Beautifulsoup to take the value of the "title" attribute and print it on the screen ?

What I have so far

import bs4, requests

link = 'http://g1.globo.com/busca/?q='
termo = 'sergio cabral' #Exemplo
completo = link + termo
pegar = requests.get(completo)
pegar.raise_for_status()
soup = bs4.BeautifulSoup(pegar.text, 'html5lib')
selec = soup.select(¯\_(ツ)_/¯)

1 answer

0

Try to use:

soup.title

In that case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.