爬天气预报

作者: Aedda | 来源:发表于2019-07-16 16:55 被阅读0次
import pandas as pd
import requests
import demjson
import time

t1=time.time()
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}
cities = {"杭州":58457,"湖州":58450,"嘉兴":58452,"金华":58549,"丽水":58646,"宁波":58465,"衢州":58633,"绍兴":58453,"台州":58651,"温州":58659,
"舟山":58477,"合肥":58321,"安庆":58424,"亳州":58102,"蚌埠":58221,"滁州":58236,"池州":58427,"阜阳":58203,"淮北":58116,"淮南":58224,"黄山":70931,
"六安":58311,"马鞍山":58336,"宿州":58122,"铜陵":58429,"芜湖":58334,"宣城":58433}
tianqi = pd.DataFrame()
for city in cities:
    url = "http://tianqi.2345.com/t/wea_forty/js/data/"+str(cities[city])+".js"
    response = requests.get(url,headers=headers)
    text = response.text
    result = text[len('''var sourceDate = "2019/6/16";var sourceData =''')+1:len(text)-2:1]
    data = demjson.decode(result)
    tianqi_new = pd.DataFrame(data)
    tianqi_new["city"] = city
    tianqi = pd.concat([tianqi,tianqi_new],ignore_index=True)
#导出数据到F盘
tianqi.to_excel(r"./天气数据.xlsx")
t2=time.time()
print(t2-t1)

相关文章

网友评论

    本文标题:爬天气预报

    本文链接:https://www.haomeiwen.com/subject/wqrnfctx.html