我的抓取代码已准备好。我唯一的问题是我无法将数据保存在CSV文件中。请帮忙吗?
我也尝试过熊猫,但没有运气。
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import csv
# open page and grab html
my_url = ('https://www.eia.gov/dnav/ng/hist/rngwhhdD.htm')
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# HTML parser
page_soup = soup(page_html, 'html.parser')
table = []
# Find table
ele_table = page_soup.find("table", summary="Henry Hub Natural Gas
Spot Price (Dollars per Million Btu)")
# traverse table
col_tag = 'th'
ele_rows = ele_table.find_all('tr', recursive=False)
for ele_row in ele_rows:
row = []
ele_cols = ele_row.find_all(col_tag, recursive=False)
for ele_col in ele_cols:
# use empty string for no data column
content = ele_col.string.strip() if ele_col.string else ''
row.append(content)
col_tag = 'td'
# just save row with data
if any(row):
table.append(row)
#open CSV file
file = open('GasPrice.csv','wb')
writer = csv.writer(file)
#print table
for row in table:
print('\t'.join(row))
#Close CSV file
file.close()
我希望将爬取的数据保存在CSV文件中。
您错过了将内容写入csv文件的过程。
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import csv
# open page and grab html
my_url = ('https://www.eia.gov/dnav/ng/hist/rngwhhdD.htm')
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# HTML parser
page_soup = soup(page_html, 'html.parser')
table = []
# Find table
ele_table = page_soup.find("table", summary="Henry Hub Natural Gas
Spot Price (Dollars per Million Btu)")
# traverse table
col_tag = 'th'
ele_rows = ele_table.find_all('tr', recursive=False)
for ele_row in ele_rows:
row = []
ele_cols = ele_row.find_all(col_tag, recursive=False)
for ele_col in ele_cols:
# use empty string for no data column
content = ele_col.string.strip() if ele_col.string else ''
row.append(content)
col_tag = 'td'
# just save row with data
if any(row):
table.append(row)
#open CSV file
file = open('GasPrice.csv','wb')
writer = csv.writer(file)
#print table
for row in table:
writer.writerow(row)
print('\t'.join(row))
#Close CSV file
file.close()