简体   繁体   中英

How to save a CSV file in row not column

Here is my code:

import requests
from bs4 import BeautifulSoup

res = requests.get("https://news.google.com/news/?ned=us&hl=en")
soup = BeautifulSoup(res.text,"html.parser")

for item in soup.select(".nuEeue"):
    news_title = (item.text)
    news_title = [news_title]
    print (news_title)
    with open('news.csv', 'a', newline='',encoding="utf-8") as f:
            writer = csv.writer(f)
            writer.writerow(news_title)
            f.close()

When I opened the csv, it shows the data in a column.

However I want to showed it in row. I tried to add end='' after print((news_title)) , but it didn't work. What should i do to make it possible?

example:
before:

a
b
c
d
e

after:

abcde

You'll have to import csv. I'm not sure how you got it to write anything.

Also, the two lines where you assign a values to news_title are a little confusing as to what you are trying to accomplish. Are you trying to get the title and the text? Maybe all of the titles?

import requests
import csv
from bs4 import BeautifulSoup

res = requests.get("https://news.google.com/news/?ned=us&hl=en")
soup = BeautifulSoup(res.text,"html.parser")

news_titles=[]  
for item in soup.select(".nuEeue"):
    news_titles.append(item.text)

    print (news_titles)
with open('news.csv', 'a') as f:
    writer csv.writer(f)
    writer.writerow(news_titles)
    f.close()

How about this?

import requests
from bs4 import BeautifulSoup

res = requests.get("https://news.google.com/news/?ned=us&hl=en")
soup = BeautifulSoup(res.text,"html.parser")

titles = [item.text for item in soup.select(".nuEeue")] #list comprehension

with open('news.csv', 'a', encoding="utf-8") as f:
    for item in titles:
        f.write(item)
        f.write(",")

However may I suggest you store your data in something else, maybe a json or a database. Here is a json alternative:

import datetime
import os
import requests
import json
from bs4 import BeautifulSoup

res = requests.get("https://news.google.com/news/?ned=us&hl=en")
soup = BeautifulSoup(res.text,"html.parser")

titles = [item.text for item in soup.select(".nuEeue") if item.text != ""] # removes blanks too
now = datetime.datetime.now().isoformat()

data = {now:titles} #creates a dictionary with key=time,value=list with titles

# Update data with old inputs if titles.json exist
if os.path.exists('titles.json'):
    with open('titles.json') as f:
        data.update(json.load(f))

# Write to titles.json
with open('titles.json',"w") as f:
    json.dump(data,f)

The json looks like this after a few runs (but with more data):

{
  "2017-09-28T04:06:55.411876": [
    "GOP proposes deep tax cuts, provides few details on how to pay for them",
    "Fact-checking Trump's claims from his speech on taxes",
    "College Student Says Car Engine Had No Oil, Hours After Getting An Oil Change"
  ],
  "2017-09-28T04:03:34.077658": [
    "GOP proposes deep tax cuts, provides few details on how to pay for them",
    "Fact-checking Trump's claims from his speech on taxes",
    "College Student Says Car Engine Had No Oil, Hours After Getting An Oil Change",
    "Benny Hinn Is My Uncle, but Prosperity Preaching Isn't for Me"
  ],
  "2017-09-28T04:01:59.304124": [
    "GOP proposes deep tax cuts, provides few details on how to pay for them",
    "Fact-checking Trump's claims from his speech on taxes",
    "Review: Apple Watch Series 3 with cellular further establishes an emerging computing platform"
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM