V
V
Vadimganin2022-03-19 14:17:53
Python
Vadimganin, 2022-03-19 14:17:53

How to save the result of the parser to a .txt file?

I have created a parser and I want to save the output to the console in a .txt file, but there is a problem.

The code:

import requests
from bs4 import BeautifulSoup as sup

page = 1

while True:
    response = requests.get("https://stopgame.ru/review/new/izumitelno/p" + str(page))
    html = sup(response.content, "html.parser")
    items = html.select(".items > .article-summary")

    if (len(items)):
        for elements in items:
            title = elements.select(".caption > a")
            res = title[0].text
            print(res)
            with open("games_list.txt", "w", encoding='utf-8') as file:
                file.write(res)

        page += 1
    else:
        break


Conclusion:
Shadow Warrior 3: Обзор
Not for Broadcast: Обзор
Strange Horticulture: Обзор
Insomnis: Обзор
Supraland Six Inches Under: Обзор
Troubleshooter: Мы зря пропустили
Wolfstride: Обзор
Fights in Tight Spaces: Обзор
Gunfire Reborn: Обзор
White Shadows: Обзор
Ruined King: A League of Legends Story: Обзор
Shin Megami Tensei V: Обзор
Corpse Party (2021): Обзор
Inscryption: Обзор
The Riftbreaker: Обзор
UNSIGHTED: Обзор
UnMetal: Обзор
Impostor Factory: Обзор
Bonfire Peaks: Обзор
Eastward: Обзор
Severed Steel: Обзор
OPUS: Echo of Starsong: Обзор
Tales of Arise: Обзор
Black Book: Обзор
И так далее


The problem is that only the last line is stored in the file, and not all the lines specified above.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2022-03-19
@Vadimganin

...
with open("games_list.txt", "a", encoding='utf-8') as file:
...

S
shurshur, 2022-03-19
@shurshur

Of course, I understand that the solution indicated by the curator of the tag will work, but generally speaking it is still wrong. It is easy to see that in this case, when the script is run again, the file will not be recreated - all the same entries will be added to it again.
Here the error is that the file is opened anew in each iteration of the loop, which in itself is unreasonable, and its contents are also overwritten. That's why it turns out one line in the end. To fix it, you need to take the opening of the file to a level higher than for. In this case, it will be opened once and closed only after the cycle is completed.

K
Konstantin Nagibovich, 2022-03-19
@nki

your_file.py > out.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question