From 8b77b419326f62a8ac1820c601df765f199af5ec Mon Sep 17 00:00:00 2001 From: "1985682903@qq.com" <1985682903@qq.com//填写自己的邮箱> Date: Fri, 14 Aug 2020 19:01:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E6=95=B0=E6=8D=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 丁香园爬虫.ipynb | 48 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/丁香园爬虫.ipynb b/丁香园爬虫.ipynb index 5d11c57..f97e5d3 100644 --- a/丁香园爬虫.ipynb +++ b/丁香园爬虫.ipynb @@ -584,7 +584,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ @@ -632,10 +632,11 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 65, "metadata": {}, "outputs": [], "source": [ + "import datetime\n", "import pymysql\n", "class MyDB:\n", " def __init__(self,host,user,passwd,db):\n", @@ -656,22 +657,48 @@ " \n", " #保存省份数据\n", " def save_province_datas(self,all_province):\n", + " \n", + " date1 = datetime.datetime.now().strftime('%Y-%m-%d')\n", + " sql1 = 'delete from province_daily_datas where pub_time like \"%s\"'%(date1 + '%')\n", + " print(sql1)\n", + " \n", + " try:\n", + " self.cursor.execute(sql1)\n", + " self.conn.commit()\n", + " print(\"之前省份删除成功\")\n", + " except Exception as a:\n", + " print(a)\n", + " \n", " sql = 'insert into province_daily_datas(provinceName,provinceShortName,currentConfirmedCount,confirmedCount,suspectedCount,curedCount,deadCount,pub_time) \\\n", " values(%s,%s,%s,%s,%s,%s,%s,%s)'\n", " res = self.get_province_list_tuple(all_province)\n", " \n", " print(\"+++++++ save_province_datas, datas len:%d\"%(len(res)))\n", " \n", - " try:\n", + " try: \n", + " \n", " self.cursor.executemany(sql,res)\n", " self.conn.commit()\n", " except Exception as e:\n", " print(e)\n", + " print(\"???\")\n", " print(\"++++++++++++ save_province_datas is over\")\n", " \n", " \n", " #保存城市数据\n", " def save_city_datas(self,all_city):\n", + " date2 = datetime.datetime.now().strftime('%Y-%m-%d')\n", + " sql2 = 'delete from city_daily_datas where pub_time like \"%s\"'%(date2 + '%')\n", + " try:\n", + " self.cursor.execute(sql2)\n", + " print((date2+\"%\"))\n", + " self.conn.commit()\n", + " print(\"之前城市删除成功\")\n", + " except Exception as a:\n", + " print(a)\n", + " \n", + " \n", + " \n", " sql = 'insert into city_daily_datas(cityName,currentConfirmedCount,confirmedCount,suspectedCount,curedCount,deadCount,locationId,province,pub_time) \\\n", " values(%s,%s,%s,%s,%s,%s,%s,%s,%s)'\n", " res = self.get_city_list_tuple(all_city)\n", @@ -679,10 +706,14 @@ " print(\"+++++++ save_city_daily_datas, datas len:%d\"%(len(res)))\n", " \n", " try:\n", + " \n", + " \n", + " \n", " self.cursor.executemany(sql,res)\n", " self.conn.commit()\n", " except Exception as e:\n", " print(e)\n", + " print(\"???\")\n", " print(\"++++++++++++ save_city_daily_datas is over\") \n", " \n", " def __del__(self):\n", @@ -703,17 +734,20 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "448\n", + "delete from province_daily_datas where pub_time like \"2020-08-14%\"\n", + "之前省份删除成功\n", "+++++++ save_province_datas, datas len:34\n", "++++++++++++ save_province_datas is over\n", - "+++++++ save_city_daily_datas, datas len:448\n", + "2020-08-14%\n", + "之前城市删除成功\n", + "+++++++ save_city_daily_datas, datas len:447\n", "++++++++++++ save_city_daily_datas is over\n" ] } @@ -837,7 +871,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.8.3" } }, "nbformat": 4,