无法跨csv文件中的列写入数据

时间:2018-08-25 14:43:07

标签: python python-3.x csv web-scraping

我已经用python编写了一个脚本,可以从网页的表格中抓取不同的名称及其值,并将其写入csv文件中。我的以下脚本可以完美地解析它们,但是我无法以自定义的方式将它们写入csv文件。

我想做的是将namesvalues分别写在图像2中。

这是我的尝试:

import csv
from bs4 import BeautifulSoup
import requests

res = requests.get("https://www.bloomberg.com/markets/stocks",headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text, "lxml")
with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    for table in soup.select(".data-table-body tr"):
        name = table.select_one("[data-type='full']").text
        value = table.select_one("[data-type='value']").text
        print(f'{name} {value}')
        writer.writerow([name,value])

输出如下:

enter image description here

我希望如何获得输出,如下所示:

enter image description here

任何解决此问题的帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

尝试定义空列表,将所有值附加到循环中,然后一次全部写入:

with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    names_and_values = []
    for table in soup.select(".data-table-body tr"):
        name = table.select_one("[data-type='full']").text
        value = table.select_one("[data-type='value']").text
        print(f'{name} {value}')
        names_and_values.extend([name,value])
    writer.writerow(names_and_values)

答案 1 :(得分:1)

如果我对您的理解正确,请尝试仅拨打一次writerow而不是每个循环一次

import csv
from bs4 import BeautifulSoup
import requests

res = requests.get("https://www.bloomberg.com/markets/stocks",headers={"User-Agent":"Mozilla/5.0"})
soup = BeautifulSoup(res.text, "lxml")
with open("outputfile.csv","w",newline="") as infile:
    writer = csv.writer(infile)
    data = []
    for table in soup.select(".data-table-body tr"):
        name = table.select_one("[data-type='full']").text
        value = table.select_one("[data-type='value']").text
        print(f'{name} {value}')
        data.extend([name, value])
    writer.writerow(data)

答案 2 :(得分:0)

这似乎是一件丑陋的事情,确定吗?

使用熊猫获取csv和操作表。您将想要执行以下操作:

import pandas as pd

df = pd.read_csv(path)
df.values.ravel()