Python 学习之股票信息爬取
2018-03-07 21:41
399 查看
技术路线:requests-bs4-re
使用场景:股票信息存储在静态页面中,非js调用输出;本例使用东方财务网、百度股票;
from bs4 import BeautifulSoup
import requests
import re
#获取html
def getHtmlText(url):
try:
r = requests.get(url, timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "获取html异常"
#解析股票list清单 http://quote.eastmoney.com/sh500029.html def resolveGupiaoList(glist, htext):
try:
soup = BeautifulSoup(htext, "html.parser")
alist = soup.find_all("a")
pat = re.compile(r'http://quote.eastmoney.com/sh\d{6}.html|http://quote.eastmoney.com/sz\d{6}.html')
for i in range(len(alist)):
if (alist[i].get('href')):
link = pat.search(alist[i].attrs['href'])
if link:
glist.append(str(link.group(0).split('/')[-1].split('.')[0]))
except:
print("resolveGupiaoList异常")
#解析个股交易详情,写入文件
def getGupiaoDetail(htext, fileAddress):
try:
gpdict = {}
soup = BeautifulSoup(htext, "html.parser")
gpname = soup.find('a', attrs={'class':'bets-name'})
if(gpname):
gpdict['股票名称'] = gpname.text.split()[0]
gpprice = soup.find('strong', attrs={'class':'_close'})
if(gpprice):
gpdict['当前股价'] = gpprice.text.split()[0]
stockInfo = soup.find('div',attrs={'class':'stock-bets'})
keyList = stockInfo.find_all('dt')
valueList = stockInfo.find_all('dd')
for i in range(len(keyList)):
key = keyList[i].text
val = valueList[i].text
gpdict[key] = val
f = open(fileAddress, 'a', encoding='utf-8')
f.write(str(gpdict) + '\n' )
except:
print("获取个股信息失败")
def main():
glist = []
baiduGupiaoUrl = "https://gupiao.baidu.com/stock/"
gupiaoListUrl = "http://quote.eastmoney.com/stocklist.html"
fileAddress = 'D:/gupiao.txt'
htext = getHtmlText(gupiaoListUrl)
resolveGupiaoList(glist, htext)
for gpitem in glist:
baiduhtext = getHtmlText(baiduGupiaoUrl + gpitem + '.html')
getGupiaoDetail(baiduhtext, fileAddress)
main()
优化方向:1、优化requests编码解析;
r.apparent_encoding 通过全文遍历url返回的html文本,解析可能使用的编码。执行效率收到影响。改进:当返回的页面内容较大,或者需要反复返回同一类页面时,可以提前确认页面的编码,从而直接赋值encoding。code="utf-8"r.encoding = code2、动态显示股票解析进度,提高用户体验。
print("\r当前进度: {:.2f}%".format(count*100/len(glist)),end="")
使用场景:股票信息存储在静态页面中,非js调用输出;本例使用东方财务网、百度股票;
from bs4 import BeautifulSoup
import requests
import re
#获取html
def getHtmlText(url):
try:
r = requests.get(url, timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "获取html异常"
#解析股票list清单 http://quote.eastmoney.com/sh500029.html def resolveGupiaoList(glist, htext):
try:
soup = BeautifulSoup(htext, "html.parser")
alist = soup.find_all("a")
pat = re.compile(r'http://quote.eastmoney.com/sh\d{6}.html|http://quote.eastmoney.com/sz\d{6}.html')
for i in range(len(alist)):
if (alist[i].get('href')):
link = pat.search(alist[i].attrs['href'])
if link:
glist.append(str(link.group(0).split('/')[-1].split('.')[0]))
except:
print("resolveGupiaoList异常")
#解析个股交易详情,写入文件
def getGupiaoDetail(htext, fileAddress):
try:
gpdict = {}
soup = BeautifulSoup(htext, "html.parser")
gpname = soup.find('a', attrs={'class':'bets-name'})
if(gpname):
gpdict['股票名称'] = gpname.text.split()[0]
gpprice = soup.find('strong', attrs={'class':'_close'})
if(gpprice):
gpdict['当前股价'] = gpprice.text.split()[0]
stockInfo = soup.find('div',attrs={'class':'stock-bets'})
keyList = stockInfo.find_all('dt')
valueList = stockInfo.find_all('dd')
for i in range(len(keyList)):
key = keyList[i].text
val = valueList[i].text
gpdict[key] = val
f = open(fileAddress, 'a', encoding='utf-8')
f.write(str(gpdict) + '\n' )
except:
print("获取个股信息失败")
def main():
glist = []
baiduGupiaoUrl = "https://gupiao.baidu.com/stock/"
gupiaoListUrl = "http://quote.eastmoney.com/stocklist.html"
fileAddress = 'D:/gupiao.txt'
htext = getHtmlText(gupiaoListUrl)
resolveGupiaoList(glist, htext)
for gpitem in glist:
baiduhtext = getHtmlText(baiduGupiaoUrl + gpitem + '.html')
getGupiaoDetail(baiduhtext, fileAddress)
main()
优化方向:1、优化requests编码解析;
r.apparent_encoding 通过全文遍历url返回的html文本,解析可能使用的编码。执行效率收到影响。改进:当返回的页面内容较大,或者需要反复返回同一类页面时,可以提前确认页面的编码,从而直接赋值encoding。code="utf-8"r.encoding = code2、动态显示股票解析进度,提高用户体验。
print("\r当前进度: {:.2f}%".format(count*100/len(glist)),end="")
相关文章推荐
- python爬虫学习 之 定向爬取 股票信息
- 初学Python的学习笔记9----面向对象编程(获取对象信息、实例属性和类属性)、面向对象高级编程(使用__slots__、使用@property)
- Python爬虫学习(一)12306余票信息爬取
- Python语言学习讲解七:使用traceback获取详细的异常信息
- Python股票信息抓取(三)
- Python网络爬虫获取股票信息
- python爬虫学习(股票数据爬取)
- python学习笔记(二)保存爬取到的信息到本地
- python爬虫由浅入深9---定向爬取股票数据信息并保存至本地文件
- Learn Python The Hard Way学习(21) - 函数可以返回信息
- Python之抓取股票信息,单继承,多继承,方法
- 【极客学院】-python学习笔记-4-单线程爬虫 (提交表单抓取信息,实战练习)
- python爬虫学习 信息标记与提取方法
- Python股票信息抓取(二)
- python实例31[urllib.request.urlopen获取股票信息]
- python自动化运维学习笔记1- psutil系统性能信息模块
- 使用python在网易爬取股票信息,制作股票分析系统(简易版)
- python学习——获取对象信息
- python学习之最简单的获取本机ip信息的小程序
- Python学习笔记15:标准库之获取进程信息(os包)