将 CSV 从 api 转换为 python 中的数据帧格式

数据挖掘 Python 数据框
2022-02-24 14:05:39

我是 python 新手,我从一个网站上提取了一些评论,我使用 webscrapping 工具的 api 将我的数据导入 python,格式为 csv。我想将此 csv 转换为 python 中的数据框。有人可以指导我如何执行此操作。

下面是导入 csv 格式的 api 提取的代码。

import requests

params = {
  "api_key": "abc",
  "format": "csv"
}
r = requests.get('https://www.parsehub.com/api/v2/runs/ttx8PT-EL6Rf/data', params=params)
print(r.text)

我对上述代码的输出如下:

"selection1_name","selection1_url","selection1_CommentID_name","selection1_CommentID_Date","selection1_CommentID_comment"
"A","https://www..html","137","February 02, 2020","I enjoy the daily package from the start with the welcoming up to the end.
I recommend this hotel."
"A","https://www.e a lot. Relaxing moments with birds chirping, different swings to chill. Overall, I shall visit again. Thanks Azuri & Marideal."
"A","https://www.html","17","June 12, 2019","Had an amazing stay for 2 nights.
The cleanliness of the room is faultless"
"B","https://www.html","133","April 16, 2019","Had a good time. Food is good."

等等...你能帮我把它转换成python中的数据框吗?

1个回答

试试下面的代码:

import requests
import pandas as pd
import io

params = {
  "api_key": "abc",
  "format": "csv"
}
r = requests.get('https://www.parsehub.com/api/v2/runs/ttx8PT-EL6Rf/data', params=params)
r = r.content
rawData = pd.read_csv(io.StringIO(r.decode('utf-8')))