Forums

How to save some results in .txt file with Python?

hello. I have this Python code, that makes a Parsing. But I want to save the results in the save.txt file. How can I do that? How to save python screen output to a text file? I am using PyScripter.

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/exercises/exercise1.html")
bsObj = BeautifulSoup(html, "html.parser")
print(bsObj.title)
print(bsObj.h1)

The following code will take care of writing to a file.

# See note 1.
with open("save.txt", mode="a") as file:  # See note 2.
    contents = file.write(f"bsObj.title: {bsObj.title}\nbsObj.h1: {bsObj.h1}")

The above with statement will take care of opening and closing the file. It should be sufficient, and obviously the layout of contents is simply pulling the attributes you wanted into the file through a combined string variable. You can re-work the attribute as your needs dictate.

Note 1: If the file does exist it will append. If there is a chance the file might not exist I'd suggest try/except error handling.

Note 2: You might want to have a sub-directory and edit the path for this file. As-is this is writing to the same directory as the python file. Also, you can also use "w" to write rather than append. "r" for read is the default attribute.

@Carewen You do not need the contents variable to write.

hello, thanks. But is save also with tags.

bsObj.title: <title>A Useful Page</title>
bsObj.h1: <h1>An Interesting Title</h1>

The output should be:

A Useful Page
An Interesting Title

ok, this is the solution, got it. Thanks. Should be also the text like {bsObj.title.text}

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/exercises/exercise1.html")
bsObj = BeautifulSoup(html, "html.parser")
print(bsObj.title)
print(bsObj.h1)

with open("save.txt", mode="w") as file:  # See note 2.
    contents = file.write(f'{bsObj.title.text}\n{bsObj.h1.text}\n{bsObj.div.text}')

@Staff fjl Yes, I don't need the contents variable to write that line. However, my brain likes clarity and readability. My goal is not ultra efficiency. I was inspired by the saying that coding should be written to ensure humans understand it first and foremost. The use of "contents" as a variable elevates readability for me. I also anticipate that opening a file will lead to work on what is being read. So far the file related projects I've worked on have required manipulation of data being read or written, thus the variable - and this learning is showing me that for the purposes of helping the variable was definitely not required. That said, seeing their solution showed me that messaging to and from the object was where the work was being done. My inexperience speaking there :)

@Melcu23 Glad to hear it. I've only been coding for a month and am currently working through a Python boot camp. I am glad that this newcomer to Python could help. It's always a great learning experience to be able support others in their learning. And as I review what you arrived at I'm being reminded that I'm learning what OOP is and how methods are how we access data in an object, and that attributes store data for an object. Always good to learn...

@Melcu23 -- glad you figured that out!

@Carewen -- no problem, thanks for sharing your experience! :)