欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python利用re,bs4,requests模塊獲取股票數(shù)據(jù)

 更新時(shí)間:2019年07月29日 14:17:28   作者:baagee  
這篇文章主要介紹了python利用re,bs4,requests模塊獲取股票數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

今天閑來(lái)無(wú)聊無(wú)意間看到了百度股票,就想著用python爬一下數(shù)據(jù),于是就找到了東方財(cái)經(jīng)網(wǎng),結(jié)合這兩個(gè)網(wǎng)站,寫(xiě)了一個(gè)小爬蟲(chóng),數(shù)據(jù)保存在文件中,比較簡(jiǎn)單的示例,就當(dāng)做用來(lái)練習(xí)正則表達(dá)式和BeautifulSoupl了。

首先頁(yè)面分析,打開(kāi)東方財(cái)經(jīng)網(wǎng)股票列表頁(yè),

和百度股票詳情頁(yè) ,右鍵查看網(wǎng)頁(yè)源代碼,

網(wǎng)址后面的代碼就是股票代碼,所以打算先獲取股票代碼,然后獲取詳情,廢話(huà)少說(shuō),直接上代碼吧:

import re
import requests
from bs4 import BeautifulSoup

#獲取html
def getHtml(url):
	try:
		req=requests.get(url)
		req.raise_for_status()
		req.encoding=req.apparent_encoding
		return req.text
	except :
		print('getHtml失敗')

#獲取股票代碼
def getStockList(lst,stockUrl):
	html=getHtml(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):
	count=0
	for stock in lst:
		url=stockUrl+stock+'.html'
		html=getHtml(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({'股票名稱(chēng)':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+=1
				print('\r當(dāng)前速度:{:.2f}%'.format(count*100/len(lst)),end='')
		except:
			count+=1
			print('\r當(dāng)前速度e:{:.2f}%'.format(count*100/len(lst)),end='')
			continue


def main():
	stockListUrl='http://quote.eastmoney.com/stocklist.html'
	stockInfotUrl='https://gupiao.baidu.com/stock/'
	outPutFile='D:\python\shuju\stockInfo.txt'
	slist=[]
	getStockList(slist,stockListUrl)
	getStockInfo(slist,stockInfotUrl,outPutFile)

main()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論