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