Python Forum
How to save some results in .txt file with Python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to save some results in .txt file with Python? (/thread-33770.html)



How to save some results in .txt file with Python? - Melcu54 - May-25-2021

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.

Quote: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)



RE: How to save some results in .txt file with Python? - bowlofred - May-25-2021

Anything you print() (which by default goes to stdout and then to your screen) can be redirected to a file.

output = "Some data for you"
print(output) # this goes to the screen

with open("/tmp/data.out", "w") as datafile:
    print(output, file=datafile) # this goes to the opened file.



RE: How to save some results in .txt file with Python? - snippsat - May-25-2021

Use Requests and not urllib,it will save you trouble if doing more parsing in future.
Using print() to save output as bowlofred show is more rare to use,but it do work.
Here how i would do it,no camelCase🐪 in Python.
import requests
from bs4 import BeautifulSoup

html = requests.get("http://www.pythonscraping.com/exercises/exercise1.html")
soup = BeautifulSoup(html.content, "html.parser")
with open('out.txt', 'w', encoding='utf-8') as f:
    f.write(f'{soup.title.text}\n{soup.h1.text}')
Output:
A Useful Page An Interesting Title
Can also look at Web-Scraping part-1, part-2.


RE: How to save some results in .txt file with Python? - Melcu54 - May-26-2021

thank you. But I must save the content of <span class> tag, extracted from the web, not my own output. That was the problem.

nameList = bsObj.findAll("span", {"class":"green"}) - this one I must save it

I try something, but is not working:

Quote:import requests
from bs4 import BeautifulSoup

html = requests.get("http://www.pythonscraping.com/exercises/exercise1.html")
bsObj = BeautifulSoup(html, "html.parser")
nameList = bsObj.findAll("span", {"class":"green"})
for name in nameList:

with open('out.txt', 'w', encoding='utf-8') as f:
f.write(f'{name.text}\n{bsObj.h1.text}')



RE: How to save some results in .txt file with Python? - snippsat - May-26-2021

(May-26-2021, 05:58 AM)Melcu54 Wrote: thank you. But I must save the content of <span class> tag, extracted from the web, not my own output. That was the problem.
There is no span tag in the link you using,are you taking about an other url address that want span tag from?
As i think you read the book Web Scraping with Python,then this is the url address used.
import requests
from bs4 import BeautifulSoup

html = requests.get("http://www.pythonscraping.com/pages/warandpeace.html")
soup = BeautifulSoup(html.content, "html.parser")
name_list = soup.find_all("span", {"class":"green"})
with open('out.txt', 'w', encoding='utf-8') as f:
    for name in name_list:
        f.write(f'{name.text}\n')