使用python爬蟲實現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo
更新時間:2018年01月05日 14:12:00 作者:OliverkingLi
下面小編就為大家分享一篇使用python爬蟲實現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
實例如下所示:
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爬蟲實現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python爬蟲回測股票的實例講解
- python基于機器學(xué)習(xí)預(yù)測股票交易信號
- 如何用Python中Tushare包輕松完成股票篩選(詳細(xì)流程操作)
- python爬取股票最新數(shù)據(jù)并用excel繪制樹狀圖的示例
- python實現(xiàn)馬丁策略回測3000只股票的實例代碼
- 基于Python爬取搜狐證券股票過程解析
- 基于Python爬取股票數(shù)據(jù)過程詳解
- 關(guān)于python tushare Tkinter構(gòu)建的簡單股票可視化查詢系統(tǒng)(Beta v0.13)
- Python爬取股票信息,并可視化數(shù)據(jù)的示例
- python用線性回歸預(yù)測股票價格的實現(xiàn)代碼
- python 簡單的股票基金爬蟲
相關(guān)文章
使用django-guardian實現(xiàn)django-admin的行級權(quán)限控制的方法
這篇文章主要介紹了使用django-guardian實現(xiàn)django-admin的行級權(quán)限控制的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
Python+OpenCV實現(xiàn)尋找到圓點標(biāo)定板的角點
這篇文章主要為大家詳細(xì)介紹了Python+OpenCV實現(xiàn)找到圓點標(biāo)定板所有點后通過距離找兩個角點,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11
python實現(xiàn)植物大戰(zhàn)僵尸游戲?qū)嵗a
這篇文章主要給大家介紹了關(guān)于python實現(xiàn)植物大戰(zhàn)僵尸游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python結(jié)合Flask框架構(gòu)建一個簡易的遠(yuǎn)程控制系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何使用Python與Flask框架構(gòu)建一個簡易的遠(yuǎn)程控制系統(tǒng),能夠遠(yuǎn)程執(zhí)行操作命令(如關(guān)機、重啟、鎖屏等),還具備實時屏幕截圖功能,需要的可以參考下2025-03-03
在Python中使用SimpleParse模塊進(jìn)行解析的教程
這篇文章主要介紹了在Python中使用SimpleParse模塊進(jìn)行解析的教程,文章來自于IBM官方的開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04

