我想从python
的字符串中删除\n
(换行符)
from bs4 import BeautifulSoup
"""
<div class="thecontent" itemprop="description"><p>Kidney stone are painful and often recurring issue that can last for weeks at a time. They are affecting around 10% of the population mostly men between ages 30 and 40. </p>\n
<h4><strong>7. Get Your Fiber</strong></h4>\n
<p>Many vegetable protein sources also have the benefit of being great sources of insoluble fiber – another effective remedy for halting the growth of existing kidney stones and preventing the formation of new ones. </p>\n
<p>While many fruits and vegetables are excellent sources of fiber, be aware that they may also contain high...</p>
</div>
"""
def get_article(html):
soup = BeautifulSoup(html, "html.parser")
e = soup.find('div', class_='thecontent').replace('\n', ' ')
print(e)
我收到此错误:
e = soup.find('div', class_='thecontent').replace('\n', ' ')
TypeError: 'NoneType' object is not callable
使用条带方法相同
答案 0 :(得分:0)
或者您可以将字符串放入数据框的列中,并使用简单的for循环正则表达式。
不要忘了像这样添加额外的反斜杠: \\ n ,因为否则它将无法正常工作。
app.get(function...)