![](/img/trans.png)
[英]Removing spaces and newlines between tags in html (aka unformatting) in python
[英]Removing newlines between tags in html in python3
我想要修剪所有的空白區域和新線條,然后轉動結果
<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是一個具有string屬性的Object,你可以使用它來訪問或修改它.
表示法,並使用str(tag)
將標記對象轉換為python str對象
文檔: 修改字符串
試試這個並根據您的用例進行修改。
desired_string = ''.join([x.strip() for x in str(soup.title).split('\r\n')])
soup.title.text.strip()
應該這樣做
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.