'unicode' 对象没有属性 'prettify'

'unicode' object has no attribute 'prettify'

我正在使用 BeautifulSoup 解析一篇 html 文章。我使用了一些功能来清除html,所以我只能保留主要文章。

此外,我想将 Soup 输出保存到文件中。我得到的错误如下:

soup = soup.prettify("utf-8")
AttributeError: 'unicode' object has no attribute 'prettify'

源代码:

#!/usr/bin/env python
import urllib2
from bs4 import BeautifulSoup
import nltk
import argparse

def cleaner():
    url = "https://www.ceid.upatras.gr/en/announcements/job-offers/full-stack-web-developer-papergo"
    ourUrl  = urllib2.urlopen(url).read()
    soup = BeautifulSoup(ourUrl)

    #remove scripts
    for script in soup.find_all('script'):
        script.extract()
    soup = soup.find("div", class_="clearfix")

    #below code will delete tags except /br
    soup = soup.encode('utf-8')
    soup = soup.replace('<br/>' , '^')
    soup = BeautifulSoup(soup)
    soup = (soup.get_text())
    soup=soup.replace('^' , '<br/>')

    print soup
    with open('out.txt','w',encoding='utf-8-sig') as f:
        f.write(soup.prettify())

if __name__ == '__main__':
    cleaner()

这是因为 soup 在这些行之后不再是 BeautifulSoupTag 实例:

soup = (soup.get_text())
soup = soup.replace('^' , '<br/>')

它变成一个 unicode 字符串,当然,它没有 .prettify() 方法。

根据您想要的输出,您应该能够使用 .get_text().replace_with(), .unwrap(), .extract() 和其他 BeautifulSoup 方法来清理您的 HTML尝试将其作为常规字符串处理。