日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用Excel和Python從互聯(lián)網(wǎng)獲取數(shù)據(jù)

今天的文章主要分為兩個部分,一是用通過Python構(gòu)建一個數(shù)據(jù)網(wǎng)站?,二是分別使用Excel和Python從編寫的Web網(wǎng)站上獲取數(shù)據(jù)。

成都創(chuàng)新互聯(lián)主要從事做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)汾陽,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

一、構(gòu)建測試用網(wǎng)站數(shù)據(jù)

通過Python Flask Web框架分別構(gòu)建一個Web網(wǎng)站和一個Web API服務(wù)。

1.構(gòu)建Web網(wǎng)站

新建一個名為“5-5-WebTable.py”的Python腳本,創(chuàng)建一個包含表格的簡單網(wǎng)頁。如果讀者對構(gòu)建方法不感興趣,可跳過以下代碼,直接執(zhí)行腳本“5-5-WebTable.py”打開網(wǎng)站。

(1)安裝flask包。

pip install flask

(2)構(gòu)建包含表格的網(wǎng)頁。

from flask import Flask

app = Flask(__name__) # 創(chuàng)建Falsk Web應(yīng)用實例

# 將路由“/”映射到table_info函數(shù),函數(shù)返回HTML代碼
@app.route('/')
def table_info():

return """

HTML表格實例,用于提供給Excel和Python讀取














……….
用戶信息表
姓名 性別 年齡
小米 22
"""

if __name__ == '__main__':
app.debug = True # 啟用調(diào)試模式
app.run() # 運行,網(wǎng)站端口默認為5000

通過命令“python ./5-5-WebTable.py”啟動網(wǎng)站,然后在瀏覽器中輸入http://127.0.0.1:5000/,出現(xiàn)如圖1所示的網(wǎng)頁內(nèi)容。

圖1  使用Flask構(gòu)建的測試網(wǎng)站

2.構(gòu)建Web API服務(wù)

新建一個名為“5-5-WebAPI.py”的Python腳本,使用flask_restplus包構(gòu)建Web API服務(wù)。如果讀者對構(gòu)建方法不感興趣,可跳過以下代碼,直接執(zhí)行腳本“5-5-WebAPI.py”打開Web API服務(wù)。

(1)安裝flask_restplus包。

pip install flask-restplus

(2)導(dǎo)入必要的庫與初始化應(yīng)用對象。

from flask import Flask
# Api類是Web API應(yīng)用的入口,需要用Flask應(yīng)用程序初始化
from flask_restplus import Api

# Resource類是HTTP請求的資源的基類
from flask_restplus import Resource

# fields類用于定義數(shù)據(jù)的類型和格式
from flask_restplus import fields

app = Flask(__name__) # 創(chuàng)建Falsk Web應(yīng)用實例

# 在flask應(yīng)用的基礎(chǔ)上構(gòu)建flask_restplus Api對象
api = Api(app, version='1.0',
title='Excel集成Python數(shù)據(jù)分析-測試用WebAPI',
description='測試用WebAPI', )

# 使用namespace函數(shù)生成命名空間,用于為資源分組
ns = api.namespace('ExcelPythonTest', description='Excel與Python Web API測試')
# 使用api.model函數(shù)生成模型對象
todo = api.model('task_model', {
'id': fields.Integer(readonly=True,
description='ETL任務(wù)唯一標(biāo)識'),
'task': fields.String(required=True,
description='ETL任務(wù)詳情')
})

(3)Web API數(shù)據(jù)操作類,包含增、刪、改、查等方法。

class TodoDAO(object):

def __init__(self):
self.counter = 0
self.todos = []

def get(self, id):
for todo in self.todos:
if todo['id'] == id:
return todo
api.abort(404, "ETL任務(wù) {} 不存在".format(id))

def create(self, data):
todo = data
todo['id'] = self.counter = self.counter + 1
self.todos.append(todo)
return todo

# 實例化數(shù)據(jù)操作,創(chuàng)建3條測試數(shù)據(jù)
DAO = TodoDAO()
DAO.create({'task': 'ETL-抽取數(shù)據(jù)操作'})
DAO.create({'task': 'ETL-數(shù)據(jù)清洗轉(zhuǎn)換'})
DAO.create({'task': 'ETL-數(shù)據(jù)加載操作'})

(4)構(gòu)建Web API的路由映射。

HTTP資源請求類從Resource類繼承,然后映射到不同的路由,同時指定可使用HTTP方法。

@ns.route('/')  # 路由“/”對應(yīng)的資源類為TodoList,可使用get方法和post方法進行請求
class TodoList(Resource):
@ns.doc('list_todos') # @doc裝飾器對應(yīng)API文檔的信息
@ns.marshal_list_with(todo) # @marshal_xxx裝飾器對模型數(shù)據(jù)進行格式轉(zhuǎn)換與輸出
def get(self): # 定義get方法獲取所有的任務(wù)信息
return DAO.todos

@ns.doc('create_todo')
@ns.expect(todo)
@ns.marshal_with(todo, code=201)
def post(self): # 定義post方法獲取所有的任務(wù)信息
return DAO.create(api.payload), 201

# 路由/對應(yīng)的資源類為Todo,可使用get、delete、put方法進行請求
@ns.route('/')
@ns.response(404, '未發(fā)現(xiàn)相關(guān)ETL任務(wù)')
@ns.param('id', 'ETL任務(wù)ID號')
class Todo(Resource):
@ns.doc('get_todo')
@ns.marshal_with(todo)
def get(self, id):
return DAO.get(id)

@ns.doc('delete_todo')
@ns.response(204, 'ETL任務(wù)已經(jīng)刪除')
def delete(self, id):
DAO.delete(id)
return '', 204

@ns.expect(todo)
@ns.marshal_with(todo)
def put(self, id):
return DAO.update(id, api.payload)

if __name__ == '__main__':
app.run(debug=True, port=8000) # 啟動Web API服務(wù),端口為8000

(4)開啟Web API服務(wù)。

通過命令“python ./5-5-WebAPI.py”啟動Web API服務(wù),在瀏覽器中輸入“http://127.0.0.1:8000/”將出現(xiàn)如圖5-23所示的Web API服務(wù)請求方法列表。

圖2  WebAPI服務(wù)請求方法列表

2、抓取用網(wǎng)頁數(shù)據(jù)

Excel可以通過“數(shù)據(jù)”選項卡下的“自網(wǎng)站”功能抓取網(wǎng)頁數(shù)據(jù)。Python可以使用 requests 庫、Beautiful Soup包、Scrapy框架抓取網(wǎng)頁數(shù)據(jù)。

1.通過Excel抓取

單擊“數(shù)據(jù)”→“自其他源”→“自網(wǎng)站”功能。Excel可讀取的網(wǎng)頁數(shù)據(jù)有局限:動態(tài)網(wǎng)頁數(shù)據(jù)無法自動識別,非表格數(shù)據(jù)無法自動識別。

(1)單擊“數(shù)據(jù)”→“自其他源”→“自網(wǎng)站”功能。

(2)確保在5.5.1節(jié)中編寫的Web網(wǎng)站已經(jīng)開啟。

(3)輸入網(wǎng)站URL地址“http://127.0.0.1:5000/”

單擊“高級”按鈕可配置更詳細的HTTP請求信息,然后單擊“確定”按鈕,如圖3所示。

圖3  配置要讀取網(wǎng)站的URL

(4)在“導(dǎo)航器”窗口中選擇導(dǎo)入數(shù)據(jù)。

如圖4所示,Excel自動識別網(wǎng)頁中的表格數(shù)據(jù),選擇表名后單擊“加載”按鈕即可。

圖4  Excel自動識別網(wǎng)頁中的表格數(shù)據(jù)

2.使用Python抓取

下面演示使用requests庫抓取整個網(wǎng)頁中的數(shù)據(jù),然后使用Beautiful Soup解析網(wǎng)頁。讀者可參考本書代碼素材文件“5-5-web.ipynb”進行學(xué)習(xí)。

(1)通過requests讀取網(wǎng)頁數(shù)據(jù)。

import requests #導(dǎo)入requests包
url ='http://127.0.0.1:5000/'

strhtml= requests.get(url) #使用get方法請求網(wǎng)頁數(shù)據(jù)

(2)通過Beautiful Soup解析網(wǎng)頁。

from bs4 import BeautifulSoup

soup = BeautifulSoup(strhtml.text) # 將網(wǎng)頁內(nèi)容作為參數(shù),創(chuàng)建soup對象
table = soup.find('table') # 查找網(wǎng)頁中的table元素
table_body = table.find('tbody') # 查找table元素中的tbody元素
data = []
rows = table_body.find_all('tr') # 查找表中的所有tr元素

for row in rows: # 遍歷數(shù)據(jù)
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
# 結(jié)果輸出:[[],
['小米', '女', '22'],['小明','男','23'],……

3、調(diào)用Web API服務(wù)

Excel可以通過“數(shù)據(jù)”選項卡下的“自網(wǎng)站”功能調(diào)用Web API服務(wù)。Python可以使用 requests 庫、Beautiful Soup包、Scrapy框架調(diào)用Web API獲取數(shù)據(jù)。

1.使用Excel調(diào)用

(1)確保5.5.1節(jié)中編寫的Web API服務(wù)已經(jīng)開啟。

(2)輸入Web API方法對應(yīng)的URL:http://127.0.0.1:8000/ExcelPythonTest/。

(3)處理返回的數(shù)據(jù)。

調(diào)用Web API服務(wù)后數(shù)據(jù)以JSON格式返回,按照5.4.3小節(jié)中介紹的方法處理JSON數(shù)據(jù)。

2.使用Python調(diào)用

使用requests庫調(diào)用Web API方法,然后對返回的JSON數(shù)據(jù)進行處理,讀者可參考本書代碼素材文件“5-5-api.ipynb”進行學(xué)習(xí)。

import requests    #導(dǎo)入requests包
url ='http://127.0.0.1:8000/ExcelPythonTest/'

strhtml= requests.get(url) #使用get方法獲取網(wǎng)頁數(shù)據(jù)

import pandas as pd

frame= pd.read_json(strhtml.text) #使用Pandas包中的read_json函數(shù)
print(frame)
#結(jié)果輸出:
id task
0 1 ETL-抽取數(shù)據(jù)操作
1 2 ETL-數(shù)據(jù)清洗轉(zhuǎn)換
2 3 ETL-數(shù)據(jù)加載操作

4、兩種方法對比

表1所示為Excel和Python抓取互聯(lián)網(wǎng)數(shù)據(jù)方法的對比。需要注意Excel從互聯(lián)網(wǎng)抓取數(shù)據(jù)的功能并不完善。

表1   Excel和Python抓取互聯(lián)網(wǎng)數(shù)據(jù)方法對比


分享文章:使用Excel和Python從互聯(lián)網(wǎng)獲取數(shù)據(jù)
新聞來源:http://www.dlmjj.cn/article/djhegsg.html