【发布时间】:2019-07-17 03:56:43
【问题描述】:
第一次发布 - 感谢任何反馈。 python 中的新用户试图获得一个从 AAA 获取国家天然气价格的刮刀。问题出现在如何将 numpy 转换为 pandas 以保留行 - 到目前为止,它正在转换为 DF 并将所有数据放在一行中。
import requests
import numpy as np
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://gasprices.aaa.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'} # This is chrome, you can set whatever browser you like
result = requests.get(url,headers = headers)
data = []
src = result.content
soup = BeautifulSoup(src,'lxml') #passing source variable into bs class to create an object
rows = soup.findAll("table", {"class": "table-mob"}) #parse box info
headers = []
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
for row in rows:
headers = row.find_all('th')
headers = [ele.text.strip() for ele in headers]
headers.append([ele for ele in headers if ele])
data = np.array(data)
df = pd.DataFrame(data)
print(df)
电流输出: enter image description here
目标是这样:
Current Avg. $2.79 $3.11 $3.36 $3.01 $2.45
Yesterday Avg. $2.79 $3.11 $3.36 $3.01 $2.45
Week Ago Avg. $2.75 $3.07 $3.32 $3.00 $2.42
Month Ago Avg. $2.69 $3.03 $3.28 $3.02 $2.33
Year Ago Avg. $2.87 $3.17 $3.42 $3.17 $2.43
【问题讨论】:
-
数据看起来如何?你能发布一个样本吗?链接
https://gasprices.aaa.com似乎不是 API 或CSV -
程序的第一部分是来自 gasprices.aaa.com 的爬虫。查看当前输出图片了解 DF 到 CSV 的样子
标签: python-3.x pandas numpy