Html page write to file

JavaScript Read and Write to Text File

In this tutorial, I will tell you about how you can read and write to text file using JavaScript. As we know JavaScript does not have the ability to access the user’s file system so for this we need to run the project on a server. To implement this we use node.js.

In node.js these are one library fs (File-System) which is used to manage all read and write operations. By using the fs module we can read and write files in both synchronous and asynchronous way.

There are many ways in which we can read and write data to file. Lets have a look on each of them one by one.

JavaScript Read and Write to Text File

Method 1: Using Node.js

First is by using writefile and readFile method in node.js environment.

This is used to write content to file. Its syntax is below:

It has three parameters path, data, callback.

Path: The path is the location of Text File. If the file is to be generated in the same folder as that of the program, then provide the name of the file only. If the file does not exist then the new file will be created automatically.

Data: Second is Data in This parameter we need to pass info that is required to write in the file.

Читайте также:  Bootstrap 4 css variables

Callback: Last one is the Callback Function in this we pass error string. If the operation fails to write the data, an error shows the fault.

To run above code run this command:

>node index.js

It is used to read the content of the file. its syntax is:

It also has three parameters path, callback, options.

path is a location of Text File. If both file and program are in a similar folder, then directly give the file name of the text file.

Second option which specifies the data is to be gathered from the file and it is optional. If you pass nothing then it returns raw buffer.

The last one is Callback function which has two more parameters (error, txtString). If at any instance it fails to load or read the file then it returns error or exception otherwise it returns all data of the file.

To run this program enter below command:

>node index.js

Method 2: Using ActiveXObject

Another method is by using a web page ActiveX objects but this method is mainly work in Internet Explorer only.

This ActiveXObject is used to return an instance of a file system library which is used with the CreateTextFile method. This JS method which generates a file defined functions and states a TextStream object to read from or write to the file. It also returns a boolean value on the basis of this we can find out that we can overwrite to the current file or not. So, if you want to add the data to the created file you can use writeline method which is used to add text string to the file.

Using ActiveX objects, the following should be included in your code to read a file:

The ActiveX object contains three things libraryname, classname, location. So, classname is the instance of a class that needs to be created. libraryname is a mandatory field and it is the sign of the app giving the object.

Источник

Writing Web Scraped HTML to a File

I was working on a project for a client where I needed to scrape data from a Web page. I wanted to save the page to a file so that I wouldn’t be making requests to the server hosting the page each time I wanted to test my code. I was using Python3 and the Requests library. When attempting to perform the write to a file, I ran into encoding issues. This task was not as straightforward as I first imagined.

My First Attempt

On my first try, I tried to save the text output of a request directly to a file.

import requests url = 'http://www.example.com' html = requests.get(url).text with open('test.html', 'w') as test_file: test_file.write(html)

I was surprised to get this error.

Traceback (most recent call last): File «.\scrape.py», line 15, in test_file.write(page.text) File «C:\Program Files\Python36\lib\encodings\cp1252.py», line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u014d’ in position 93579: character maps to

Strike Two

Okay, so there’s an encoding error. Python represents characters using Unicode, which essentially assigns a bit value to each character. Unicode can be implemented using different characters sets ( charset s). The most popular character set on the Web is UTF-8 , which stores characters in 1 to 4 8-bit bytes.

So, I decided I’d tell python to encode my text using UTF-8.

import requests url = 'http://www.example.com' html = requests.get(url).text with open('test.html', 'w') as test_file: page = str(html, encoding='utf-8') test_file.write(page)

Third Time’s a Charm?

Another surprise… I thought with the str method I would be able to transform the Unicode into UTF-8 encoded string, yet I received the following traceback.

Traceback (most recent call last): File ".\scrape.py", line 15, in page = str(html, encoding='utf-8') TypeError: decoding str is not supported

Apparently the str method doesn’t work on an str object. Okay… since Unicode works on the byte level, let’s try a bytes object.

import requests url = 'http://www.example.com' html = requests.get(url).text with open('test.html', 'w') as test_file: page = str(bytes(html), encoding='utf-8') test_file.write(page)
Traceback (most recent call last): File ".\scrape.py", line 15, in page = str(bytes(html), encoding='utf-8') TypeError: string argument without an encoding

The Solution

I looked up the str class in the Python manual, but unfortunately that didn’t help me much – I like Python, but IMHO their docs are a tad overly terse. While on the docs, I followed the link to the builtin bytes function. I discovered it too had an argument for encoding. So I thought, let’s try that.

import requests url = 'http://www.example.com' html = requests.get(url).text with open('test.html', 'w') as test_file: page = bytes(html, 'utf-8') test_file.write(page)
Traceback (most recent call last): File ".\scrape.py", line 16, in test_file.write(page) TypeError: write() argument must be str, not bytes

I was able to transform the HTML text into UTF-8 encoded bytes, but I forgot to turn the bytes back into a Python string to be able to write to the open test.html file. That’s easy to do.

import requests url = 'http://www.example.com' html = requests.get(url).text with open('test.html', 'w') as test_file: page = bytes(html, 'utf-8') test_file.write(str(page))

Finally, I was able to grab the page from the Web and write it to a file.

Источник

Оцените статью