Home > Software design >  How to convert Python's BeautifulSoup .text to uppercase?
How to convert Python's BeautifulSoup .text to uppercase?

Time:05-13

I am attempting to webscrape the titles from articles on the BBC website and convert it to uppercase through the python .upper method. I'm using Python's BeautifulSoup library for this. This is my code:

from bs4 import BeautifulSoup as bs
import requests

url = 'https://www.bbc.co.uk/news/world-60525350'

html = requests.get(url)

soup = bs(html.text, "html.parser")

article_box = soup.find_all('div', class_='gel-layout__item gs-u-pb @m gel-1/3@m gel-1/4@xl gel-1/3@xxl nw-o-keyline nw-o-no-keyline@m')

for titles in article_box:
    string = titles.text
    upper = string.upper
    print(upper,'\n\n')  

The output that I'm receiving when attempting this is:

<built-in method upper of str object at 0x104dce870>

What am I missing here? I thought that BeautifulSoup's .text produced strings.

CodePudding user response:

You have to use .upper() method

upper = string.upper()

CodePudding user response:

You need to call a method upper to have an uppercase result.

for titles in article_box:
    string = titles.text
    upper = string.upper
    print(upper(),'\n\n') 
  • Related