I am building a database that collects the news published on a newspaper website following instructions from this code https://github.com/jhnwr/webscrapenewsarticles/blob/master/newscraper.py.. John Watson Rooney github site But when I extract the information doing web scraping process, the output is inside brackets "[]" and I can't remove them to clean the data and make a news dataframe
'''
#find all the articles by using inspect element and create blank list
n=0
newslist = []
#loop through each article to find the title, subtitle, link, date and author. try and except as repeated articles from other sources have different h tags.
for item in articles:
try:
newsitem = item.find('h3', first=True)
title = newsitem.text
link = newsitem.absolute_links
subtitle = item.xpath('//a[@class="epigraph page-link"]//text()')
author = item.xpath('//span[@class="oculto"]/span//text()')
date = item.xpath('//meta[@itemprop="datePublished"]/@content')
date_scrap = dt.datetime.utcnow().strftime("%d/%b/%Y")
hour_scrap = dt.datetime.utcnow().strftime("%H:%M:%S")
print(n, '\n', title, '\n', subtitel, '\n', link, '\n', author, '\n', date, '\n', date_scrap , '\n', hour_scrap)
newsarticle = {
'title': title,
'subtitle': subtitle,
'link': link,
'autor': author,
'fecha': date,
'date_scrap': dat_scrap,
'hour_scrap': hour_scrap
}
newslist.append(newsarticle)
n+=1
except:
pass
news_db = pd.DataFrame(rows)
news_db.to_excel (r'db_article.xlsx', index = False, header=True)
news_db.head(10)
'''
I'm not allowed to embed image, but printing output is like:
En Vivo Procuraduría y Fiscalía investigan caso de joven que se
suicidó tras detención
['Una joven de 17 años denunció que 4
policías la agredieron sexualmente durante las protestas']
{'https://www.eltiempo.com/justicia/investigacion/investigan-denuncia-de-agresion-sexual-de-policias-a-menor-en-popayan-588429'}
['Here_Author_name']
['2021-05-14']
15/May/2021
18:14:48
I would like to remove both type brackets "[]" y "{}", I have used the following commands but they convert the values in NAN:
news_db['subtitle']= news_bd['subtitle'].str.strip(']')
news_db['subtitle']= news_bd['subtitle']..str.replace(r"\[.*\]", "")