您好,登錄后才能下訂單哦!
本文實例講述了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"])
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設(shè)計入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。