« »
2008-05-28Python

43

python多线程抓新浪天气的代码

  1. # -*- coding: utf-8 -*-
  2. import os,sys,time,re
  3. from threading import Thread
  4.  
  5. class DownloadWeather(Thread):
  6.     def __init__(self, path, url, num_of_workers=5, timeout = 2):
  7.         Thread.__init__(self)
  8.         self.path = path
  9.         self.url = url
  10.         #self.city = city/var/www/weather/data/' + city
  11.     url='http://weather.sina.com.cn http://php.weather.sina.com.cn/js2.php?city=' +city+ '&time=' +wtime
  12. <span id="more-298"></span>
  13.     # an instance of class DownloadWeather
  14.     mydownload=DownloadWeather(path,url)
  15.     #mydownload=DownloadWeather(path,city,wtime)
  16.     mydownload.setDaemon(1)
  17.     mydownload.start()
  18. mydownload.join()
  19. f.close()
  20. print (time.time() - time1)
  21.  
  22.     def run(self):
  23.         #use command "curl" download data from sina.com.cn
  24.         os.popen("/usr/local/bin/curl --limit-rate 200k --compressed -s -o %s -e %s" % (self.path, self.url))
  25.  
  26. time1=time.time()
  27. f = open("/var/www/weather/city.txt")
  28. for city in f.readlines():
  29.     city=city.strip()
  30.     wtime=str(time.time())[:10]
  31.     path='

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

您还可能感兴趣的内容

日志信息 »

该日志于2008-05-28 15:47由 x72 发表在Python分类下, 通告目前不可用,你可以至底部留下评论。

没有评论

发表评论 »


返回顶部