使用python爬蟲實(shí)現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo
實(shí)例如下所示:
import requests from bs4 import BeautifulSoup import traceback import re def getHTMLText(url): try: r = requests.get(url) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return "" def getStockList(lst, stockURL): html = getHTMLText(stockURL) soup = BeautifulSoup(html, 'html.parser') a = soup.find_all('a') for i in a: try: href = i.attrs['href'] lst.append(re.findall(r"[s][hz]\d{6}", href)[0]) except: continue def getStockInfo(lst, stockURL, fpath): for stock in lst: url = stockURL + stock + ".html" html = getHTMLText(url) try: if html=="": continue infoDict = {} soup = BeautifulSoup(html, 'html.parser') stockInfo = soup.find('div',attrs={'class':'stock-bets'}) name = stockInfo.find_all(attrs={'class':'bets-name'})[0] infoDict.update({'股票名稱': name.text.split()[0]}) keyList = stockInfo.find_all('dt') valueList = stockInfo.find_all('dd') for i in range(len(keyList)): key = keyList[i].text val = valueList[i].text infoDict[key] = val with open(fpath, 'a', encoding='utf-8') as f: f.write( str(infoDict) + '\n' ) except: traceback.print_exc() continue def main(): stock_list_url = 'http://quote.eastmoney.com/stocklist.html' stock_info_url = 'https://gupiao.baidu.com/stock/' output_file = 'D:/BaiduStockInfo.txt' slist=[] getStockList(slist, stock_list_url) getStockInfo(slist, stock_info_url, output_file) main()
優(yōu)化并且加入進(jìn)度條顯示
import requests from bs4 import BeautifulSoup import traceback import re def getHTMLText(url, code="utf-8"): try: r = requests.get(url) r.raise_for_status() r.encoding = code return r.text except: return "" def getStockList(lst, stockURL): html = getHTMLText(stockURL, "GB2312") soup = BeautifulSoup(html, 'html.parser') a = soup.find_all('a') for i in a: try: href = i.attrs['href'] lst.append(re.findall(r"[s][hz]\d{6}", href)[0]) except: continue def getStockInfo(lst, stockURL, fpath): count = 0 for stock in lst: url = stockURL + stock + ".html" html = getHTMLText(url) try: if html == "": continue infoDict = {} soup = BeautifulSoup(html, 'html.parser') stockInfo = soup.find('div', attrs={'class': 'stock-bets'}) name = stockInfo.find_all(attrs={'class': 'bets-name'})[0] infoDict.update({'股票名稱': name.text.split()[0]}) keyList = stockInfo.find_all('dt') valueList = stockInfo.find_all('dd') for i in range(len(keyList)): key = keyList[i].text val = valueList[i].text infoDict[key] = val with open(fpath, 'a', encoding='utf-8') as f: f.write(str(infoDict) + '\n') count = count + 1 print("\r當(dāng)前進(jìn)度: {:.2f}%".format(count * 100 / len(lst)), end="") except: count = count + 1 print("\r當(dāng)前進(jìn)度: {:.2f}%".format(count * 100 / len(lst)), end="") continue def main(): stock_list_url = 'http://quote.eastmoney.com/stocklist.html' stock_info_url = 'https://gupiao.baidu.com/stock/' output_file = 'BaiduStockInfo.txt' slist = [] getStockList(slist, stock_list_url) getStockInfo(slist, stock_info_url, output_file) main()
以上這篇使用python爬蟲實(shí)現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python爬蟲回測股票的實(shí)例講解
- python基于機(jī)器學(xué)習(xí)預(yù)測股票交易信號(hào)
- 如何用Python中Tushare包輕松完成股票篩選(詳細(xì)流程操作)
- python爬取股票最新數(shù)據(jù)并用excel繪制樹狀圖的示例
- python實(shí)現(xiàn)馬丁策略回測3000只股票的實(shí)例代碼
- 基于Python爬取搜狐證券股票過程解析
- 基于Python爬取股票數(shù)據(jù)過程詳解
- 關(guān)于python tushare Tkinter構(gòu)建的簡單股票可視化查詢系統(tǒng)(Beta v0.13)
- Python爬取股票信息,并可視化數(shù)據(jù)的示例
- python用線性回歸預(yù)測股票價(jià)格的實(shí)現(xiàn)代碼
- python 簡單的股票基金爬蟲
相關(guān)文章
Pytorch distributed 多卡并行載入模型操作
這篇文章主要介紹了Pytorch distributed 多卡并行載入模型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼
本文主要介紹了pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02Python GUI自動(dòng)化實(shí)現(xiàn)繞過驗(yàn)證碼登錄
這篇文章主要介紹了python GUI自動(dòng)化實(shí)現(xiàn)繞過驗(yàn)證碼登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Python連接Oracle數(shù)據(jù)庫的操作指南
Oracle數(shù)據(jù)庫是一種強(qiáng)大的企業(yè)級(jí)關(guān)系數(shù)據(jù)庫管理系統(tǒng)(RDBMS),而Python是一門流行的編程語言,兩者的結(jié)合可以提供出色的數(shù)據(jù)管理和分析能力,本教程將詳細(xì)介紹如何在Python中連接Oracle數(shù)據(jù)庫,并演示常見的數(shù)據(jù)庫任務(wù),需要的朋友可以參考下2023-11-11對(duì)python 判斷數(shù)字是否小于0的方法詳解
今天小編就為大家分享一篇對(duì)python 判斷數(shù)字是否小于0的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例
今天小編就為大家分享一篇python numpy生成等差數(shù)列、等比數(shù)列的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02