欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python實(shí)現(xiàn)抓取中國(guó)銀行外匯牌價(jià)首頁數(shù)據(jù)實(shí)現(xiàn)

 更新時(shí)間:2021年10月28日 14:25:12   作者:BoBo啵啵  
這篇文章主要為大家介紹了如何使用python實(shí)現(xiàn)抓取中國(guó)銀行外匯牌價(jià)首頁數(shù)據(jù)的實(shí)現(xiàn)示例,有需要的同學(xué)可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步

利用requests、BeautifulSoup、xlwings庫(kù)抓取中國(guó)銀行外匯牌價(jià)首頁數(shù)據(jù)

1. 利用requests、BeautifulSoup、xlwings庫(kù)抓取中國(guó)銀行外匯牌價(jià)首頁數(shù)據(jù)。

(1)中國(guó)銀行外匯牌價(jià)網(wǎng)址如下。

https://www.bankofchina.com/sourcedb/whpj/

在這里插入圖片描述

(2)調(diào)用requests模塊中g(shù)et方法訪問上述網(wǎng)址,獲取Response 對(duì)象。

url = "https://www.bankofchina.com/sourcedb/whpj/"
web=re.get(url)

(3)利用BeautifulSoup類解析。

#BeautifulSoup將字節(jié)流轉(zhuǎn)換為utf-8編碼
bs_obj=BeautifulSoup(web.text,'lxml')

(4)利用find_all方法查找table、tr、td等標(biāo)簽對(duì)象。

#查找數(shù)據(jù)所在表格

table=bs_obj.find_all('table')[1]
all_th=all_tr.find_all('th')
#print(all_th)
all_td=all_tr.find_all('td')
#print(all_td)

(5)將找到的相應(yīng)標(biāo)簽內(nèi)容依次添加到列表中。

if len(all_th)>0:
        dataRow=[]
        for item in all_th:
            dataRow.append(item.text)
        dataAll.extend([dataRow])
    if len(all_td)>0:
        dataRow=[]
        for item in all_td:
            dataRow.append(item.text)
        dataAll.extend([dataRow])

(6)利用xlwings庫(kù),將列表內(nèi)容寫入Excel文件。

wb=xw.Book()
sht=wb.sheets('Sheet1')
sht.range('a1').value=dataAll#將數(shù)據(jù)添加到表格中

(7)利用這部分?jǐn)?shù)據(jù)建立折線圖。

chart=sht.charts.add(500,50,700,400)
chart.set_source_data(sht.range('A1:A28,C1:C28,E1:E28'))#設(shè)置數(shù)據(jù)畫圖
chart.chart_type='line_markers'

chart.name='line_markersd'
#chart.api[1].ChartTitle.Text='中國(guó)銀行外匯牌價(jià)'

Code:

import requests as re
from bs4 import BeautifulSoup
import xlwings as xw
url = "https://www.bankofchina.com/sourcedb/whpj/"
web=re.get(url)
web.encoding=web.apparent_encoding
#BeautifulSoup將字節(jié)流轉(zhuǎn)換為utf-8編碼
bs_obj=BeautifulSoup(web.text,'lxml')
#查找數(shù)據(jù)所在表格
table=bs_obj.find_all('table')[1]
#print(table)
dataAll=[]
for all_tr in table.find_all('tr'):#找到所有tr,返回一個(gè)列表
    all_th=all_tr.find_all('th')
    #print(all_th)
    all_td=all_tr.find_all('td')
    #print(all_td)
    if len(all_th)>0:
        dataRow=[]
        for item in all_th:
            dataRow.append(item.text)
        dataAll.extend([dataRow])
    if len(all_td)>0:
        dataRow=[]
        for item in all_td:
            dataRow.append(item.text)
        dataAll.extend([dataRow])
wb=xw.Book()
sht=wb.sheets('Sheet1')
sht.range('a1').value=dataAll#將數(shù)據(jù)添加到表格中
chart=sht.charts.add(500,50,700,400)
chart.set_source_data(sht.range('A1:A28,C1:C28,E1:E28'))#設(shè)置數(shù)據(jù)畫圖
chart.chart_type='line_markers'
chart.name='line_markersd'
#chart.api[1].ChartTitle.Text='中國(guó)銀行外匯牌價(jià)'

在這里插入圖片描述

以上就是使用python實(shí)現(xiàn)抓取中國(guó)銀行外匯牌價(jià)首頁數(shù)據(jù)實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python抓取中國(guó)銀行外匯牌價(jià)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論