简体   繁体   中英

save html content into a txt file using python3

I'm tired of searching and trying codes that give repetitive errors, I really hope someone will help me figure this out. my probleme is so simple I'm trying to save an html code in a txt file using python, here's the code I'm using:

from urllib.request import urlopen as uReq
url1 = 'http://www.marmiton.org/recettes/menu-de-la-semaine.aspx'
page = uReq(url1).read().decode()
f = open("test.html", "w")
f.write(page)
f.close()

but it's giving me the following error:

UnicodeEncodeError: 'charmap' codec can't encode character '\♥' in position 416224: character maps to

Here is the updated solution:

Python 2.x:

import urllib

url1 = 'http://www.marmiton.org/recettes/menu-de-la-semaine.aspx'
page = urllib.urlopen(url1).read()
f = open("./test1.html", "w")
f.write(page)
f.close()

Python 3.x:

import urllib.request
import shutil

url1 = 'http://www.marmiton.org/recettes/menu-de-la-semaine.aspx'
page = urllib.request.urlopen(url1)
print(page)
f = open("./test2.html", "wb")
shutil.copyfileobj(page, f)
f.close()

You need to use urllib to help you achieve this task.

You should try with requests and bs4 (BeautifulSoup)

from bs4 import BeautifulSoup
import requests
r = requests.get("https://stackoverflow.com/questions/47503845/save-html-content-into-a-txt-file-using-python")
data = r.text
soup = BeautifulSoup(data)
print(soup)
with open ('/tmp/test.html', 'a') as f:
    f.write(str(soup))

You mention that by not using the .decode() method gives you A Type Error. Have you try to take the HTML content and pass it to the write() method as a string. You may find the way to enclose the HTML content with triple quotes, so you pass it as a multiline string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM