2

我希望得到您的建议/帮助:

创建一个python脚本:

将从网站收集的数据添加到 CSV 文件的新行中。

规则:

  • 该脚本必须在您的计算机上自动运行 5 天。

你有什么建议吗?:(

感谢您在这方面的帮助。

我试过这个:

import urllib2
import cookielib

cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))

setURL = 'http://www.wunderground.com/global/stations/54511.html?MR=1'
request = urllib2.Request(setURL)
response = opener.open(request)

url = "http://www.wunderground.com/global/stations/54511.html?MR=1"
request = urllib2.Request(url)
page = opener.open(request)

WeatherData = page.read()                            
print WeatherData

所以它打印所有数据,但我只想打印:

日期时间(捕获数据的时间戳) - 当前条件 - 温度

  • 就像我说的需要建议一样:

    • 我应该用什么来完成这个任务。

    • 如何设置(天)的数据收集,我不知道..

我不需要完整的答案和复制粘贴,我不是傻瓜......

我想了解。

4

2 回答 2

2

Weather Underground 发布了一个Python 示例来访问他们的 API。我认为你最好使用他们的代码。如果您parsed_json在示例中使用变量,您应该能够得到您想要的。

至于在固定间隔后运行程序,请查看此线程

于 2013-07-16T04:04:05.857 回答
2

这种任务称为屏幕抓取我在下面显示的代码只是为非常基本的清理添加了一些字符串操作例程,但是您可以使用用于屏幕抓取的工具(例如Beautiful Soup)做得更好。

import urllib2
import cookielib

cookieJar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))

url = "http://www.wunderground.com/global/stations/54511.html?MR=1"
request = urllib2.Request(url)
page = opener.open(request)

# This is one big string
rawdata = page.read()

# This breaks it up into lines
lines_of_data = rawdata.split('\n')

# This is one line in the raw data that looks interesing.  I'm going to
# filter the raw data based on the "og:title" text.
# 
#'<meta name="og:title" content="Beijing, Beijing | 31&deg; | Clear" />

# The "if line.find(" bit is the filter. 
special_lines = [line for line in lines_of_data if line.find('og:title')>-1]
print special_lines

# Now we clean up - this is very crude, but you can improve it with
# exactly what you want to do.
info = special_lines[0].replace('"','').split('content=')[1]
sections = info.split('|')
print sections

输出:

['\t\t<meta name="og:title" content="Beijing, Beijing | 32&deg; | Clear" />']
['Beijing, Beijing ', ' 32&deg; ', ' Clear />']

编辑: 无论如何,如果特定网站提供像 Xaranke 回答中的 JSON 这样的 Web 服务,请使用它!但并非所有网站都这样做,所以 Beautiful Soup 仍然非常有用。

于 2013-07-16T04:45:31.957 回答