您的位置:首页 > 编程语言 > Python开发

基于python实时抓取广州市pm2.5数据

2019-03-19 10:50 281 查看

本文主要参考https://blog.csdn.net/beiniao520/article/details/79582443
代码是基于python3.7参考上述博客编写的

import threading
import re,sys
import time
import hashlib
import os
from urllib import request

sys.setdefaultencoding='utf-8'

def fetchdata(city):
md5=''
while True:
temp='http://www.pm25.in/'+ city
url=request.urlopen(temp)
text=url.read()
reg='<td>(.*?)</td>'
comreg=re.compile(reg,re.S)
shuju=re.findall(comreg,text.decode('utf-8'))#需要将text解码成字符串
data_time = re.findall("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}",text.decode('utf-8'),re.S)
md52=hashlib.md5()
md52.update(data_time[0].encode('utf-8'))#参数是bytes类型,所以要进行编码
if md52.hexdigest()==md5:
time.sleep(3600)
contiune

md5=md52.hexdigest()
i=1
data=[]
dataname=data_time[0]
dataname1=dataname.replace(':','')
tempdata=open('G:/desktop/'+dataname1+'.txt','a')
#创建并打开文件准备写入数据,每小时生成一个文件

for each in shuju:
data.append(each)
i+=1
if i>10:
data.append(data_time[0])
i=1
tempdata.write(','.join(data)+'\\n')
data=[]

tempdata.close()#写完文件关闭
print (city)
print (data_time[0])
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
time.sleep(3600)

fetchdata('guangzhou')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: