Chinaunix首页 | 论坛 | 博客
  • 博客访问: 353914
  • 博文数量: 42
  • 博客积分: 3970
  • 博客等级: 中校
  • 技术积分: 714
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-13 15:31
文章分类

全部博文(42)

文章存档

2009年(19)

2008年(23)

我的朋友

分类: Python/Ruby

2008-03-05 14:01:04

# -*- coding: utf-8 -*-
import os,sys,time,re
from threading import Thread

class DownloadWeather(Thread):
    def __init__(self, path, url, num_of_workers=5, timeout = 2):
        Thread.__init__(self)
        self.path = path
        self.url = url
        #self.city = city

    def run(self):
        #use command "curl" download data from sina.com.cn
        os.popen("/usr/local/bin/curl --limit-rate 200k --compressed -s -o %s -e %s" % (self.path, self.url))

time1=time.time()
f = open("/var/www/weather/city.txt")
for city in f.readlines():
    city=city.strip()
    wtime=str(time.time())[:10]
    path='
/var/www/weather/data/' + city
    url=' ' +city+ '&time=' +wtime
    # an instance of class DownloadWeather
    mydownload=DownloadWeather(path,url)
    #mydownload=DownloadWeather(path,city,wtime)
    mydownload.setDaemon(1)
    mydownload.start()
mydownload.join()
f.close()
print (time.time() - time1)

读出/var/www/weather/city.txt的文件,里面一行是一个城市的名称,也是从天气页面下载的,注意,这个文件必须是gb2312编码的,不然无法curl正确的数据,只是因为url编码的问题。
阅读(1018) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:python通过表单的post方法自动用户登陆

给主人留下些什么吧!~~