为什么我的 CSV 不保存抓取的数据?

Why is my CSV not saving the data scraped?

我的抓取代码已准备就绪。我遇到的唯一问题是无法将数据保存在 CSV 文件中。请帮忙?

我也尝试过 panda,但没有成功。

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()