使用python實(shí)現(xiàn)抓取中國(guó)銀行外匯牌價(jià)首頁數(shù)據(jù)實(shí)現(xià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)文章
一個(gè)基于flask的web應(yīng)用誕生 flask和mysql相連(4)
一個(gè)基于flask的web應(yīng)用誕生第四篇,這篇文章主要介紹了如何讓flask和mysql進(jìn)行互聯(lián),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04python中print()函數(shù)的“,”與java中System.out.print()函數(shù)中的“+”功能詳解
python中的print()函數(shù)和java中的System.out.print()函數(shù)都有著打印字符串的功能。接下來通過本文給大家分享python中print()函數(shù)的“,”與java中System.out.print()函數(shù)中的“+”功能,需要的朋友參考下吧2017-11-11Python Web開發(fā)模板引擎優(yōu)缺點(diǎn)總結(jié)
這篇文章主要介紹了Python Web開發(fā)模板引擎優(yōu)缺點(diǎn)總結(jié),需要的朋友可以參考下2014-05-05詳解Django的CSRF認(rèn)證實(shí)現(xiàn)
這篇文章主要介紹了詳解Django的CSRF認(rèn)證實(shí)現(xiàn),詳細(xì)的介紹了csrf原理和實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10Python的hashlib庫(kù)獲取超大文件的md5值實(shí)例探究
這篇文章主要為大家介紹了Python的hashlib庫(kù)獲取超大文件的md5值實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01使用Keras實(shí)現(xiàn)Tensor的相乘和相加代碼
這篇文章主要介紹了使用Keras實(shí)現(xiàn)Tensor的相乘和相加代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06