大數(shù)據(jù)獲取案例:Python網(wǎng)絡(luò)爬蟲(chóng)實(shí)例
網(wǎng)絡(luò)爬蟲(chóng):
網(wǎng)絡(luò)爬蟲(chóng)(又稱為網(wǎng)頁(yè)蜘蛛,網(wǎng)絡(luò)機(jī)器人,在FOAF社區(qū)中間,更經(jīng)常的稱為網(wǎng)頁(yè)追逐者),是一種按照一定的規(guī)則,自動(dòng)地抓取萬(wàn)維網(wǎng)信息的程序或者腳本。另外一些不常使用的名字還有螞蟻、自動(dòng)索引、模擬程序或者蠕蟲(chóng)。
以上是網(wǎng)絡(luò)爬蟲(chóng)的百度,下面開(kāi)始介紹使用Python進(jìn)行網(wǎng)絡(luò)爬蟲(chóng)來(lái)獲取數(shù)據(jù)。
用來(lái)獲取新冠肺炎的實(shí)時(shí)數(shù)據(jù)。
使用的工具PyCharm
新建Python文件,命名為get_data
使用爬蟲(chóng)最常用的request模塊
第一部分:
獲取網(wǎng)頁(yè)信息:
import requests url = "https://voice.baidu.com/act/newpneumonia/newpneumonia"response = requests.get(url)
第二部分:
可以觀察數(shù)據(jù)的特點(diǎn):
數(shù)據(jù)包含在script標(biāo)簽里,使用xpath來(lái)獲取數(shù)據(jù)。
導(dǎo)入一個(gè)模塊from lxml import etree
生成一個(gè)html對(duì)象并且進(jìn)行解析
可以得到一個(gè)類型為list的內(nèi)容,使用第一項(xiàng)就可以得到全部?jī)?nèi)容
接下來(lái)首先獲取component的內(nèi)容,這時(shí)使用json模塊,將字符串類型轉(zhuǎn)變?yōu)樽值?Python的數(shù)據(jù)結(jié)構(gòu))
為了獲取國(guó)內(nèi)的數(shù)據(jù),需要在component中找到caseList
接下來(lái)上代碼:
from lxml import etreeimport json# 生成HTML對(duì)象html = etree.HTML(response.text)
result = html.xpath('//script[@type="application/json"]/text()')
result = result[0]# json.load()方法可以將字符串轉(zhuǎn)化為python數(shù)據(jù)類型result = json.loads(result)
result_in = result['component'][0]['caseList']
第三部分:
將國(guó)內(nèi)的數(shù)據(jù)存儲(chǔ)到excel表格中:
使用openyxl模塊,import openpyxl
首先創(chuàng)建一個(gè)工作簿,在工作簿下創(chuàng)建一個(gè)工作表
接下來(lái)給工作表命名和給工作表賦予屬性
代碼如下:
import openpyxl#創(chuàng)建工作簿wb = openpyxl.Workbook()#創(chuàng)建工作表ws = wb.active
ws.title = "國(guó)內(nèi)疫情"ws.append(['省份', '累計(jì)確診', '死亡', '治愈', '現(xiàn)有確診', '累計(jì)確診增量', '死亡增量', '治愈增量', '現(xiàn)有確診增量'])'''
area --> 大多為省份
city --> 城市
confirmed --> 累計(jì)
crued --> 值域
relativeTime -->
confirmedRelative --> 累計(jì)的增量
curedRelative --> 值域的增量
curConfirm --> 現(xiàn)有確鎮(zhèn)
curConfirmRelative --> 現(xiàn)有確鎮(zhèn)的增量
'''for each in result_in:
temp_list = [each['area'], each['confirmed'], each['died'], each['crued'], each['curConfirm'],
each['confirmedRelative'], each['diedRelative'], each['curedRelative'],
each['curConfirmRelative']] for i in range(len(temp_list)): if temp_list[i] == '':
temp_list[i] = '0'
ws.append(temp_list)
wb.save('./data.xlsx')
第四部分:
將國(guó)外數(shù)據(jù)存儲(chǔ)到excel中:
在component的globalList中得到國(guó)外的數(shù)據(jù)
然后創(chuàng)建excel表格中的sheet即可,分別表示不同的大洲
代碼如下:
data_out = result['component'][0]['globalList']for each in data_out:
sheet_title = each['area'] # 創(chuàng)建一個(gè)新的工作表
ws_out = wb.create_sheet(sheet_title)
ws_out.append(['國(guó)家', '累計(jì)確診', '死亡', '治愈', '現(xiàn)有確診', '累計(jì)確診增量']) for country in each['subList']:
list_temp = [country['country'], country['confirmed'], country['died'], country['crued'],
country['curConfirm'], country['confirmedRelative']] for i in range(len(list_temp)): if list_temp[i] == '':
list_temp[i] = '0'
ws_out.append(list_temp)
wb.save('./data.xlsx')
整體代碼如下:
import requestsfrom lxml import etreeimport jsonimport openpyxl
url = "https://voice.baidu.com/act/newpneumonia/newpneumonia"response = requests.get(url)#print(response.text)# 生成HTML對(duì)象html = etree.HTML(response.text)
result = html.xpath('//script[@type="application/json"]/text()')
result = result[0]# json.load()方法可以將字符串轉(zhuǎn)化為python數(shù)據(jù)類型result = json.loads(result)#創(chuàng)建工作簿wb = openpyxl.Workbook()#創(chuàng)建工作表ws = wb.active
ws.title = "國(guó)內(nèi)疫情"ws.append(['省份', '累計(jì)確診', '死亡', '治愈', '現(xiàn)有確診', '累計(jì)確診增量', '死亡增量', '治愈增量', '現(xiàn)有確診增量'])
result_in = result['component'][0]['caseList']
data_out = result['component'][0]['globalList']'''
area --> 大多為省份
city --> 城市
confirmed --> 累計(jì)
crued --> 值域
relativeTime -->
confirmedRelative --> 累計(jì)的增量
curedRelative --> 值域的增量
curConfirm --> 現(xiàn)有確鎮(zhèn)
curConfirmRelative --> 現(xiàn)有確鎮(zhèn)的增量
'''for each in result_in:
temp_list = [each['area'], each['confirmed'], each['died'], each['crued'], each['curConfirm'],
each['confirmedRelative'], each['diedRelative'], each['curedRelative'],
each['curConfirmRelative']] for i in range(len(temp_list)): if temp_list[i] == '':
temp_list[i] = '0'
ws.append(temp_list)# 獲取國(guó)外疫情數(shù)據(jù)for each in data_out:
sheet_title = each['area'] # 創(chuàng)建一個(gè)新的工作表
ws_out = wb.create_sheet(sheet_title)
ws_out.append(['國(guó)家', '累計(jì)確診', '死亡', '治愈', '現(xiàn)有確診', '累計(jì)確診增量']) for country in each['subList']:
list_temp = [country['country'], country['confirmed'], country['died'], country['crued'],
country['curConfirm'], country['confirmedRelative']] for i in range(len(list_temp)): if list_temp[i] == '':
list_temp[i] = '0'
ws_out.append(list_temp)
wb.save('./data.xlsx')
結(jié)果如下:
國(guó)內(nèi):

國(guó)外:

推薦 :
020 持續(xù)更新,精品小圈子每日都有新內(nèi)容,干貨濃度極高。
結(jié)實(shí)人脈、討論技術(shù) 你想要的這里都有!
搶先入群,跑贏同齡人!(入群無(wú)需任何費(fèi)用)
群號(hào):858157650
申請(qǐng)即送:
Python軟件安裝包,Python實(shí)戰(zhàn)教程
資料免費(fèi)領(lǐng)取,包括 Python基礎(chǔ)學(xué)習(xí)、進(jìn)階學(xué)習(xí)、爬蟲(chóng)、人工智能、自動(dòng)化運(yùn)維、自動(dòng)化測(cè)試等

工程師必備
- 項(xiàng)目客服
- 培訓(xùn)客服
- 平臺(tái)客服
TOP




















