How to save a web page as a text file [Python]

I would like to save the web page (all content) as a text file. (As if you made a right click on a web page -> "Save Page As" -> "Save As Text File", and not as an html file)

I tried using the following code:

import urllib2
url=''
page = urllib2.urlopen(url)
page_content = page.read()
file = open('file_text.txt', 'w')
f.write(page_content)
f.close()

My goal is to save all text without html code. (for example, I would like to read "รจ" "& eacute" instead)

+4
source share
1 answer

See html2text as mentioned elsewhere

import urllib2
import html2text
url=''
page = urllib2.urlopen(url)
html_content = page.read()
rendered_content = html2text.html2text(html_content)
file = open('file_text.txt', 'w')
file.write(rendered_content)
file.close()
+2
source

Source: https://habr.com/ru/post/1627075/


All Articles