題目:
程式設計爬取每日最新的疫情統計資料。
並將爬取結果匯入到資料庫中。
將視覺化結果與統計資料結合,實時顯示當前最新資料。
這次的作業與上次周的視覺化可以整合成乙個完整的**,只需要在這次加上python爬取資料即可
爬取的**如下
1加上上次的**,效果如下圖所示:from os import
path
2import
requests
3from bs4 import
beautifulsoup
4import
json
5import
pymysql6#
import numpy as np
7import
time
8from _ast import
try9
10 url = ''#
請求位址
11 headers = #
建立頭部資訊
12 response = requests.get(url,headers = headers) #
傳送網路請求13#
print(response.content.decode('utf-8'))#以位元組流形式列印網頁原始碼
14 content = response.content.decode('
utf-8')
15#print(content)
16 soup = beautifulsoup(content, '
html.parser')
17 lista = soup.find_all(name='
script
',attrs=)18#
世界確診
19 listb = soup.find_all(name='
script
',attrs=)20#
lista = soup.find_all(name='div',attrs=)
21 account =str(lista)
22 world_messages = str(listb)[87:-21]
23 messages = account[52:-21]
24 messages_json =json.loads(messages)
25 world_messages_json =json.loads(world_messages)
26 valueslist =
27 citylist =
28 worldlist =
29 localtime =time.localtime(time.time())
30 l=time.strftime("
%y-%m-%d %h:%m:%s
", time.localtime())
31print
(l)32
for i in
range(len(messages_json)):33#
value = messages_json[i]34#
value = (messages_json[i].get('provincename'),messages_json[i].get('provinceshortname'),messages_json[i].get('currentconfirmedcount'),messages_json[i].get('confirmedcount'),messages_json[i].get('suspectedcount'),messages_json[i].get('curedcount'),messages_json[i].get('deadcount'),messages_json[i].get('comment'),messages_json[i].get('locationid'))
35 value = (messages_json[i].get('
provincename
'),messages_json[i].get('
confirmedcount
'),messages_json[i].get('
curedcount
'),messages_json[i].get('
deadcount
'),messages_json[i].get('
locationid'))
3637 cityvalue = messages_json[i].get('
cities')
38#print(cityvalue) 乙個省內沒有劃分開的值
39for j in
range(len(cityvalue)):40#
cityvaluelist = (cityvalue[j].get('cityname'),cityvalue[j].get('currentconfirmedcount'),cityvalue[j].get('confirmedcount'),cityvalue[j].get('suspectedcount'),cityvalue[j].get('curedcount'),cityvalue[j].get('deadcount'),cityvalue[j].get('locationid'),messages_json[i].get('provinceshortname'))
41 cityvaluelist = (messages_json[i].get('
provincename
'),cityvalue[j].get('
cityname
'),cityvalue[j].get('
confirmedcount
'),cityvalue[j].get('
curedcount
'),cityvalue[j].get('
deadcount
'),cityvalue[j].get('
locationid'))
42#print(cityvaluelist) 省份內各個城市的值
434445#
print(citylist) #城市46#
print(valueslist) #省份
47 db=pymysql.connect("
localhost
","root
","123456
","payiqing
", charset='
utf8')
48 cursor =db.cursor()
4950 sql_city="
insert into info_copy (province,city,confirmed_num,cured_num,dead_num,code,date) values (%s,%s,%s,%s,%s,%s,'
"+l+"')"
51 sql_province="
insert into info_copy (province,confirmed_num,cured_num,dead_num,code,date) values (%s,%s,%s,%s,%s,'
"+l+"')"
52#print(sql)
5354 value_tuple=tuple(valueslist)
55 city_tuple=tuple(citylist)
5657
try:
58cursor.executemany(sql_province,valueslist)
59cursor.executemany(sql_city,city_tuple)
60db.commit()
61except:62
print('
執行失敗,進入**4')
63db.rollback()
6465
6667 db.close()
缺陷記錄日誌
Python爬取疫情資料
實現 輸出結果 import requests 請求庫 import pandas as pd 日期 today time.strftime y m d time.localtime format y 年 m 月 d 日 url 請求頭 headers 傳送請求 response requests....
搜狗 疫情資料爬取(Python)
上週已經分享過搜狗 疫情資料爬取 r語言 這次分享一下搜狗 疫情資料爬取 python from urllib import request from lxml import etree import re import pandas as pd import json url response r...
爬蟲 爬取騰訊疫情資料
網頁結構 實現 爬取的資料 結語右鍵檢查,分析網頁 找到我們需要的資料所在的 找到 下面就是相關 實現了。首先匯入python相關庫 requests 網頁請求,獲取原始資料 json 網頁解析,去除多餘字元 pandas 資料處理 import requests import json impor...