我想修剪掉所有的空格和新行并将结果从
<title>
Asian Case Research Journal (World Scientific)
</title>
到此
<title>Asian Case Research Journal (World Scientific)</title>
我的代码:
for link in url_list:
try:
r = requests.get(link)
soup = BeautifulSoup(r.content,"html.parser")
print(soup.title)
except:
print("No Title Found ")
continue
最佳答案
import bs4
html = '''<title>
Asian Case Research Journal (World Scientific)
</title>'''
soup = bs4.BeautifulSoup(html, 'lxml')
title = soup.title
title.string = title.get_text(strip=True)
print(str(title))
输出:
<title>Asian Case Research Journal (World Scientific)</title>
在bs4中,tag是一个具有字符串属性的Object,可以使用.
表示法来访问或修改它,并使用str(tag)将tag对象转换为python str对象
关于python - 删除python3中html标签之间的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288624/