Python код записывает CSV-файл, печатает только два текстовых ввода - PullRequest
0 голосов
/ 17 апреля 2020
#Stuff needed to run
import requests
import urllib.request
import io
from bs4 import BeautifulSoup as soup

#Pick url, request it, save response, read response, soup it into variable
my_url = 'https://old.reddit.com/r/all/'
request = urllib.request.Request(my_url,headers={'User-Agent': 'your bot 0.1'})
response = urllib.request.urlopen(request)
page_html = response.read()
page_soup = soup(page_html, "html.parser")

#get all the posts, get one post, get all the authors, get one author
posts = page_soup.findAll("div", {"class": "top-matter"})
post = posts[0]
authors = page_soup.findAll("p", {"class":"tagline"})
author = authors[0]

#make filename, open to write, set the headers, write the headers,
filename = "redditAll.csv"
f = open(filename, "w")
headers = "Title of the post, Author of the post\n"
f.write(headers)

#for the post and author in posts and authors, get one of each, open the file & write it, repeat
for post, author in zip(posts, authors):
    post_text = post.p.a.text.replace(",", " -")
    username = author.a.text
    with open(filename, "w", encoding="utf-8") as f:
        f.write(post_text + "," + username + "\n")

#close the file
f.close()

После запуска этого кода и открытия файла csv в тексте есть только две ячейки.

Их должно быть больше двух, так как на reddit.com более двух сообщений. / r / all

Изменено это

for post, author in zip(posts, authors):
    post_text = post.p.a.text.replace(",", " -")
    username = author.a.text
    with open(filename, "w", encoding="utf-8") as f:
        f.write(post_text + "," + username + "\n")

На это

with open(filename, "w", encoding="utf-8") as f:
    for post, author in zip(posts, authors):
        post_text = post.p.a.text.replace(",", " -")
        username = author.a.text
        f.write(post_text + "," + username + "\n")

Ответы [ 3 ]

0 голосов
/ 17 апреля 2020

Вы можете открыть файл в режиме добавления, используя параметр a, во второй раз, когда вы открываете запись в файл, проверьте этот SO поток о том, как это сделать. Или переместите with open(filename, "w", encoding="utf-8") as f: за пределы l oop

Параметр w перезапишет предыдущие данные в файле, поэтому при каждом запуске l oop запись будет перезаписываться новой записью оставив только окончательную запись в файле

Также я бы go со встроенной библиотекой csv для чтения / записи CSV-файлов, как упоминается в одном из комментариев. Здесь - документация для него

0 голосов
/ 17 апреля 2020

Изменено это

for post, author in zip(posts, authors):
    post_text = post.p.a.text.replace(",", " -")
    username = author.a.text
    with open(filename, "w", encoding="utf-8") as f:
        f.write(post_text + "," + username + "\n")

На это

with open(filename, "w", encoding="utf-8") as f:
    for post, author in zip(posts, authors):
        post_text = post.p.a.text.replace(",", " -")
        username = author.a.text
        f.write(post_text + "," + username + "\n")
0 голосов
/ 17 апреля 2020

Попробуйте это:

# for the post and author in posts and authors, get one of each, open the file & write it, repeat
def writer():
    with open(filename, "w", encoding="utf-8") as f:
        for post_, author_ in zip(posts, authors):
            post_text = post_.p.a.text.replace(",", " -")
            username = author_.a.text
            # with open(filename, "w", encoding="utf-8") as f:
            f.write(post_text + "," + username + "\n")

writer()

...