Python Forum
How to save some results in .txt file with Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save some results in .txt file with Python?
#1
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)
Reply
#2
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.
Melcu54 and Pedroski55 like this post
Reply
#3
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.
Melcu54 and Pedroski55 like this post
Reply
#4
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}')
Reply
#5
(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')
Melcu54 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Want to Save Print output in csv file Rasedul 5 218 Jan-11-2022, 07:04 PM
Last Post: snippsat
  How to save Matplot chart to temp file? Morkus 2 1,396 Jun-12-2021, 10:52 AM
Last Post: Morkus
  How to save modification of a file in original file not a new one? mjrezayani 3 1,301 Jun-04-2021, 01:28 PM
Last Post: snippsat
  How to design a save file format? philipbergwerf 5 1,662 Apr-26-2021, 07:39 PM
Last Post: Gribouillis
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 1,919 Jan-14-2021, 08:58 PM
Last Post: snippsat
  Writing unit test results into a text file ateestructural 3 1,542 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  capture pytest results to a file maiya 2 1,696 Oct-17-2020, 03:42 AM
Last Post: maiya
  Compiling Python 3.8.5 source code results in build error Deepan 0 1,096 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 941 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Searching string in file and save next line dani8586 2 1,004 Jul-10-2020, 09:03 AM
Last Post: dani8586

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020