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

本文主要参考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')
        

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值