文件保存.txt中的BeautifulSoup错误

时间:2016-03-16 18:57:46

标签: python save beautifulsoup

from bs4 import BeautifulSoup
import requests
import os


url = "http://nos.nl/artikel/2093082-steeds-meer-nekklachten-bij-kinderen-door-gebruik-tablets.html"
r  = requests.get(url)
soup = BeautifulSoup(r.content.decode('utf-8', 'ignore'))
data = soup.find_all("article", {"class": "article"})

with open("data1.txt", "wb") as file:
   content=‘utf-8’
for item in data:
    content+='''{}\n{}\n\n{}\n{}'''.format( item.contents[0].find_all("time", {"datetime": "2016-03-16T09:50:30+0100"})[0].text,
                                            item.contents[0].find_all("a", {"class": "link-grey"})[0].text,
                                            item.contents[0].find_all("img", {"class": "media-full"})[0],
                                            item.contents[1].find_all("div", {"class": "article_textwrap"})[0].text,
                                            )
with open("data1.txt".format(file_name), "wb") as file:
    file.write(content)

最近解决了utf / Unicode问题,但现在它没有将其保存为.txt文件,也没有保存它。我需要做什么?

1 个答案:

答案 0 :(得分:0)

如果您想将数据作为UTF-8写入文件,请尝试codecs.open

from bs4 import BeautifulSoup
import requests
import os
import codecs


url = "http://nos.nl/artikel/2093082-steeds-meer-nekklachten-bij-kinderen-door-gebruik-tablets.html"
r  = requests.get(url)
soup = BeautifulSoup(r.content)
data = soup.find_all("article", {"class": "article"})

with codecs.open("data1.txt", "wb", "utf-8") as filen:
    for item in data:
        filen.write(item.contents[0].find_all("time", {"datetime": "2016-03-16T09:50:30+0100"})[0].get_text())
        filen.write('\n')
        filen.write(item.contents[0].find_all("a", {"class": "link-grey"})[0].get_text())
        filen.write('\n\n')
        filen.write(item.contents[0].find_all("img", {"class": "media-full"})[0].get_text())
        filen.write('\n')
        filen.write(item.contents[1].find_all("div", {"class": "article_textwrap"})[0].get_text())

我不确定filen.write(item.contents[0].find_all("img", {"class": "media-full"})[0]),因为它为我返回了Tag个实例。