代码运行成功,但没有结果

时间:2017-03-12 09:56:13

标签: python

我是python的新手,我正在学习如何使用本书来抓取数据" Visualize This"作者:Nathan Yau。我试图在2016年刮掉Wunderground。我的代码运行成功,但在wunder-data.txt中没有任何结果。我很困惑。

以下是我的代码:

import sys
import urllib2
from bs4 import BeautifulSoup as BS
from codecs import open

# Create/open a file called wunder.txt (which will be a comma-delimited file)
#open('wunder-data.txt', 'w', encoding='utf8')

# Iterate through months and day
for m in range(1, 13):
for d in range(1, 32):

    # Check if already gone through month
    if (m == 2 and d > 28):
        break
    elif (m in [4, 6, 9, 11] and d > 30):
        break

# Open wunderground.com url
url = "https://www.wunderground.com/history/airport/KMHK/2016/" + str(m) + "/" + str(d) + "/DailyHistory.html"
page = urllib2.urlopen(url)

# Get temperature from pagef
soup = BS(page, "html.parser")
# dayTemp = soup.body.nobr.b.string
dayTemp = soup.find("span", text="Mean Temperature").parent.find_next_sibling("td").get_text(strip=True)

# Format month for timestamp
if len(str(m)) < 2:
    mStamp = '0' + str(m)
else:
    mStamp = str(m)

# Format day for timestamp
if len(str(d)) < 2:
    dStamp = '0' + str(d)
else:
    dStamp = str(d)

# Build timestamp
timestamp = '2016' + mStamp + dStamp

# Write timestamp and temperature to file
#s = (timestamp + ',' + dayTemp + '\n').encode('utf-8')
# or, cleaner:
s = u'{},{}\n'.format(timestamp, dayTemp).encode('utf-8')

# Done getting data! Close file.

2 个答案:

答案 0 :(得分:1)

靠近文件顶部, 你有这个:

open('wunder-data.txt', 'w', encoding='utf8')

这将打开一个用于写入的文件并向其返回一个文件句柄。 但是你还没有存储文件句柄, 所以你不能写信给它。 您发布的代码中没有任何内容可以写入此文件。

它还不清楚你想写什么文件。 如果它是您在脚本末尾设置的s的值, 然后删除顶部的open(...)电话, 并在最后设置s后添加此项:

with open('wunder-data.txt', 'w', encoding='utf8') as fh:
    fh.write(s.decode('utf8'))

答案 1 :(得分:1)

with open('wunder-data.txt', 'w', encoding='utf8') as file_descriptor:
    # do something to obtain the data
    # and do not forget to write the result to the file_descriptor:
    file_descriptor.write("My desired string.")
    # otherwise it is not suprising to be confused
相关问题