【发布时间】:2015-12-03 23:24:34
【问题描述】:
我遇到了一些问题。首先,当我尝试从网络抓取中写入 CSV 文件时,没有写入任何内容。该文件确实保存,但它完全是空白的。最终,我希望打开它并调用水温柱来计算平均值。
我的另一个问题是我只想要 CSV 文件中表中的几列。有人可以验证我所做的是否正确吗?我只想要前 3 列,然后是第 14 列。
谢谢!
import sys
import urllib2
import csv
import requests
from bs4 import BeautifulSoup
r_temp1 = requests.get('http://www.ndbc.noaa.gov/data/realtime2/BZBM3.txt')
html_temp1 = r_temp1.text
soup = BeautifulSoup(html_temp1, "html.parser")
table_temp1 = soup.find('table')
rows_temp1 = table.findAll('tr')
rows_temp1 = rows_temp1[1:]
#writing to a csv file
csvfile_temp1 = open("temp1.csv","wb")
output_temp1 = csv.writer(csvfile_temp1, delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL)
for row in rows_temp1:
Year = cells[0].text.strip()
Month = cells[1].text.strip()
Day = cells[2].text.strip()
W_temp = cells[14].text.strip()
output.writerow([Year,Month,Day,W_temp])
csvfile_temp1.close()
【问题讨论】:
标签: python csv web-scraping