これで、bs4(BeautifulSoup)を使用してウェブサイトを解析し、探している特定のタイトルを探しています。bs4を使用してテキスト上の空白を取り除く方法
import requests
from bs4 import BeautifulSoup
url = 'http://www.ewn.co.za/Categories/Local'
r = requests.get(url).text
soup = BeautifulSoup(r)
for i in soup.find_all(class_='article-short'):
if i.a:
print(i.a.text.replace('\n', '').strip())
else:
print(i.contents[0].strip())
このコードは動作しますが、出力には、それはウェブサイトから要求されたタイトルを印刷する前に、最初の空白の20行のように示しています。私のコードは次のようになります。私のコードに何か間違いがあるのですか、空白を取り除くために何かできるのでしょうか?
あなたは(https://docs.python.org/3/library/stdtypes.html#str.strip) – Querenker