我正在嘗試從 BBC 網站上的文章中抓取標題,并通過 python .upper 方法將其轉換為大寫。為此,我正在使用 Python 的 BeautifulSoup 庫。這是我的代碼:
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')
我在嘗試這樣做時收到的輸出是:
<0x104dce870 處 str 物件的內置方法上部>
我在這里想念什么?我認為 BeautifulSoup 的 .text 產生了字串。
uj5u.com熱心網友回復:
你必須使用.upper()方法
upper = string.upper()
uj5u.com熱心網友回復:
您需要呼叫方法 upper 以獲得大寫的結果。
for titles in article_box:
string = titles.text
upper = string.upper
print(upper(),'\n\n')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473906.html
下一篇:正則運算式匹配字串和特殊字符
