爬取天气(python)

import pandas as pd
import requests
import demjson
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'}
cook = {"Cookie":'UM_distinctid=168ac0f9119565-044e6209c82f7-57b143a-100200-168ac0f911a588; fingerprint=4263213985; _ga=GA1.2.885880429.1551944653; dj_star_ticket=03a4870f2d42b9699ea2449d8fe96d300001652000; com.xiaoju.ecom.mis.login.sso.cookie.key=A0EfpzXG1TNzgluSddBZiSHrUUoJVLR72GKbu%2F5uUNVzcX6ydZdDwxVx1M11amFRF6chwVSj5Pk6%0A3ValqKhe2J2iDvBhI4B5; omgh5sid=640921997642-1553061738689'}
ym = ['201701','201702','201703','201704','201705','201706','201707','201708','201709','201710','201711','201712','201801','201802','201803','201804','201805','201806','201807','201808','201809','201810','201811','201812','201901','201902','201903']
df = pd.DataFrame()
for i in ym:
    url = "https://tianqi.2345.com/t/wea_history/js/"+i+"/58659_"+i+".js"
    response = requests.get(url, cookies=cook, headers=headers)
    text = response.text
    result = text[len("var weather_str="):len(text)-1:1]
    data = demjson.decode(result)
    for i in range(len(data['tqInfo'])-1):
        df_new = pd.DataFrame(data['tqInfo'][i],index=['a'])
        df = pd.concat([df,df_new],ignore_index=True)
df.to_excel("J:\温州天气数据.xlsx")

你可能感兴趣的:(python)