Python代码写入csv文件,仅打印两个文本条目

问题描述 投票:0回答:3
#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")
python csv writing
3个回答
0
投票
尝试一下:

0
投票
您可以使用a参数以附加模式打开文件,第二次打开文件写入时,请检查此SO thread的操作方法。或将with open(filename, "w", encoding="utf-8") as f:移到循环外

0
投票
更改此
© www.soinside.com 2019 - 2024. All rights reserved.