Skip to content Skip to sidebar Skip to footer

Beautifulsoup Error In File Saving .txt

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

Solution 1:

If you want to write the data as UTF-8 to the file try codecs.open like:

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 indata:
        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())

I'm unsure about filen.write(item.contents[0].find_all("img", {"class": "media-full"})[0]) because that returned a Tag instance for me.

Post a Comment for "Beautifulsoup Error In File Saving .txt"