本文實例講述了Python獲取基金網站網頁內容、使用BeautifulSoup庫分析html操作。分享給大家供大家參考,具體如下:
利用 urllib包 獲取網頁內容
#引入包 from urllib.request import urlopen response = urlopen("http://fund.eastmoney.com/fund.html") html = response.read(); #這個網頁編碼是gb2312 #print(html.decode("gb2312")) #把html內容保存到一個文件 with open("1.txt","wb") as f: f.write(html.decode("gb2312").encode("utf8")) f.close()
使用BeautifulSoup分析html
from bs4 import BeautifulSoup # 讀取文件內容 with open("1.txt", "rb") as f: html = f.read().decode("utf8") f.close() # 分析html內容 soup = BeautifulSoup(html,"html.parser") # 取出網頁title print(soup.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() },) # 打印結果 print(result[0]["name"])
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》、《Python+MySQL數據庫程序設計入門教程》及《Python常見數據庫操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
