-1
好的,所以我使用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