liuyuqi-dellpc 3 years ago
commit
dccde8a1e3
4 changed files with 168 additions and 0 deletions
  1. 15 0
      README.md
  2. 45 0
      crawl_electricity.py
  3. 106 0
      electricity_analysis.ipynb
  4. 2 0
      requirements.txt

+ 15 - 0
README.md

@@ -0,0 +1,15 @@
+# crawl_electricity
+
+全球电力可视化分析。
+
+crawl_electricity.py    爬取数据
+
+electricity_analysis.ipynb     分析数据
+
+
+
+```
+pip install requiremets.txt
+
+
+```

+ 45 - 0
crawl_electricity.py

@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2021/10/10 07:24:07
+@License :   Copyright © 2017-2020 liuyuqi. All Rights Reserved.
+@Desc    :   全球电力数据爬虫
+https://www.iea.org/fuels-and-technologies/electricity
+'''
+import re
+import sys
+import os
+import time
+import requests
+import pandas as pd
+import numpy as np
+
+headers = {
+    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
+res = requests.get(r'https://api.iea.org/stats/countries', headers=headers)
+
+# 获取所有国家
+countries = pd.DataFrame(res.json())
+countries = countries.dropna(subset=["region"])
+
+dataList = []
+for i, country in enumerate(countries["stats"]):
+    params = {
+        "countries": country,
+        "startYear": 1990, }
+
+    res = requests.get(
+        r'https://api.iea.org/stats/indicator/TotElecCons', headers=headers, params=params)
+    df = pd.DataFrame(res.json())
+    try:
+        data = df[["year"]].copy()
+        data["country"] = df["value"]
+        data.set_index("year", inplace=True)
+        dataList.append(data)
+        print(f"{i}")
+    except:
+        continue
+
+results = pd.concat(dataList, axis=1).fillna(0).loc[:"2018"]
+results.to_csv("results.csv")

+ 106 - 0
electricity_analysis.ipynb

@@ -0,0 +1,106 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "source": [
+    "## 中国限电舆情数据分析\r\n",
+    "\r\n",
+    "国庆的到来,各地区都出台各种限电措施,各种“专家”纷纷揣度圣意。\r\n",
+    "\r\n",
+    "1、有专家大言不惭,美国大量印钞,而中国没有同步跟上,导致贬值的美元可以大量买走中国商品,限电是吾皇为了和美国贸易战下的一盘大棋,目的就是提高商品价格,争夺定价权。可笑之极。。。\r\n",
+    "\r\n",
+    "2、有些专家迎合大众口味,面对即将过年的能耗达标,北京中央出台文件点名批评各耗电大省。限电是各地方政府为了碳排放达标,而在年终前的赶作业行为。\r\n",
+    "\r\n",
+    "3、当然还有很多其他“专家”大言不惭,中国电价太便宜,限电是为了即将电价大涨的前兆。\r\n",
+    "\r\n",
+    "**限电的原因其实很简单,就是产电不足。而产电不足的核心原因就是煤炭供应不够,北京中央已经下发文件,内蒙古优质煤矿开采迅速审批,发电厂能发尽发。**\r\n"
+   ],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [
+    "import os,sys,re\r\n",
+    "import numpy as np\r\n",
+    "import pandas as pd\r\n",
+    "import matplotlib.pyplot as plt"
+   ],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [
+    "df=pd.read_csv(\"result.csv\")\r\n"
+   ],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "source": [],
+   "outputs": [],
+   "metadata": {}
+  }
+ ],
+ "metadata": {
+  "orig_nbformat": 4,
+  "language_info": {
+   "name": "python"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+requests
+pandas