Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作示例
本文實例講述了Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作。分享給大家供大家參考,具體如下:
利用 urllib包 獲取網(wǎng)頁內(nèi)容
#引入包 from urllib.request import urlopen response = urlopen("http://fund.eastmoney.com/fund.html") html = response.read(); #這個網(wǎng)頁編碼是gb2312 #print(html.decode("gb2312")) #把html內(nèi)容保存到一個文件 with open("1.txt","wb") as f: f.write(html.decode("gb2312").encode("utf8")) f.close()
使用BeautifulSoup分析html
from bs4 import BeautifulSoup # 讀取文件內(nèi)容 with open("1.txt", "rb") as f: html = f.read().decode("utf8") f.close() # 分析html內(nèi)容 soup = BeautifulSoup(html,"html.parser") # 取出網(wǎng)頁title print(soup.title) #<title>每日開放式基金凈值表 _ 天天基金網(wǎng)</title> # 基金編碼 codes = soup.find("table",id="oTable").tbody.find_all("td","bzdm") result = () # 初始化一個元組 for code in codes: result += ({ "code":code.get_text(), "name":code.next_sibling.find("a").get_text(), "NAV":code.next_sibling.next_sibling.get_text(), "ACCNAV":code.next_sibling.next_sibling.next_sibling.get_text() },) # 打印結(jié)果 print(result[0]["name"])
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
如何使用django的MTV開發(fā)模式返回一個網(wǎng)頁
這篇文章主要介紹了如何使用django的MTV開發(fā)模式返回一個網(wǎng)頁,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07python數(shù)據(jù)操作之lambda表達式詳情
這篇文章主要介紹了python數(shù)據(jù)操作之lambda表達式詳情,文章基于python的相關資料展開lambda表達式具體的內(nèi)容,感興趣的小伙伴可以參考一下2022-05-05Python中的collections集合與typing數(shù)據(jù)類型模塊
這篇文章介紹了Python中的collections集合與typing數(shù)據(jù)類型模塊,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05