【发布时间】:2020-01-28 14:08:44
【问题描述】:
我有这个网站,我从那里将数据抓取为 CSV 文件。我能够刮掉日期和价格。但是日期是周格式,我需要将其转换为日期格式,例如 5 个工作日的每日价格。 (周一至周六)。我为此使用了蟒蛇和熊猫以及美味的汤。 WHAT I GET AND WHAT I WANT FROM THIS SITE 从 urllib.request 导入 urlopen
from urllib.error import HTTPError
from urllib.error import URLError
from bs4 import BeautifulSoup
from pandas import DataFrame
import csv
import pandas as pd
from urllib.request import urlopen
尝试:
html = urlopen("https://www.eia.gov/dnav/ng/hist/rngwhhdD.htm")
HTTPError 除外:
print(e)
除了 URLError:
print("Server down or incorrect domain")
其他:
res = BeautifulSoup(html.read(),"html5lib")
price = res.findAll(class_=["tbody", "td", "B3"])
price_list = []
for tag in price:
price_tag=tag.getText()
price_list.append(price_tag)
print(price_tag)
date = res.findAll(class_=["tbody", "td", "B6"])
date_list = []
for tag in date:
date_tag=tag.getText()
date_list.append(date_tag)
print(date_tag)
d1 = pd.DataFrame({'Date': date_list})
d2 = pd.DataFrame({'Price': price_list})
df = pd.concat([d1,d2], axis=1)
print(df)
df.to_csv("Gas Price.csv", index=False, header=True)
【问题讨论】:
标签: python python-3.x pandas web-scraping beautifulsoup