如何在行{而不是列

时间:2017-09-28 01:58:33

标签: python csv python-requests

这是我的代码:

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()

当我打开csv时,它会在列中显示数据。

但是我想要排成一行。 我尝试在end=''之后添加print((news_title)),但它无效。我该怎么做才能成为可能?

例如:
之前:

一个
b
ç
d
Ë

后:

ABCDE

2 个答案:

答案 0 :(得分:0)

您必须导入csv。我不确定你是怎么写它的。

此外,您为news_title分配值的两行对于您要完成的操作有点令人困惑。你想获得标题和文字吗?也许所有的头衔?

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()

答案 1 :(得分:0)

这个怎么样?

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(",")

但是,我建议您将数据存储在其他地方,可能是json或数据库。 这是json的另类选择:

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)

json在几次运行后看起来像这样(但有更多数据):

{
  "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"
    ]
}