我的代码输出多个空换行符。 我如何删除所有空白区域?
from bs4 import BeautifulSoup
import urllib.request
import re
url = input('enter url moish')
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page,'lxml')
all = soup.find_all('a', {'class' : re.compile('itemIncludes')})
for i in all:
print(i.text)
代码输出:
Canon EOS 77D DSLR Camera (Body Only)
LP-E17 Lithium-Ion Battery Pack
LC-E17 Charger for LP-E17 Battery Pack
期望的输出:
Canon EOS 77D DSLR Camera (Body Only)
LP-E17 Lithium-Ion Battery Pack
LC-E17 Charger for LP-E17 Battery Pack
谢谢!
最佳答案
您可以在打印前删除空行:
items = [item.text for item in all if item.text.strip() != '']
关于python - 使用 Beautifulsoup 抓取时如何删除多个空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55483772/