python 东方财富网&百度股票数据定向爬虫 实例
来源:互联网 发布:速卖通数据分析软件 编辑:程序博客网 时间:2024/04/29 13:07
功能:
1、 获取 上交所 深 交所的股票信息
2、 输出保存到文件中
技术路线: requests -beatiful soup - re
候选:数据网站选择
1、静态网站,信息静态存在HTML页面中,非js 代码生成
2、 F12 , 源代码查看
多找信息源
方法:
1、 从东方财富网获取 股票列表信息
2、 根据股票列表逐个到百度股票 获取个股信息
3、 将结果存储到文件中
import requestsfrom bs4 import BeautifulSoupimport tracebackimport redef 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: continuedef 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() continuedef 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()
改进代码 —-1、添加进度条,增加用户体验
2、
import requestsfrom bs4 import BeautifulSoupimport tracebackimport redef 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: continuedef 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当前进度: {:.2f}%".format(count*100/len(lst)),end="") except: count = count + 1 print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="") continuedef 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()
0 0
- python 东方财富网&百度股票数据定向爬虫 实例
- 东方财富网股票数据爬虫
- python爬虫案例——东方财富股票数据采集
- 【Python爬虫】东方财富数据爬取
- 网络爬虫之东方财富网股票板块
- Python网络爬虫与信息提取-Day14-(实例)股票数据定向爬虫
- 股票数据定向爬虫
- 爬虫实例(三)——股票数据定向爬虫
- Python 爬虫实战(2):股票数据定向爬虫
- 基于正则表达式(python)对东方财富网上证指数吧爬虫实例
- Jsoup读取东方财富网站数据实例
- python抓取东方财富网股票的公告的里面的链接
- 获取股票简单数据:腾讯、新浪、东方财富。。。
- [Python爬虫]爬取东方财富网公司公告需要注意的几个问题
- python爬虫实战二——股票数据定向爬虫【有补充】
- 【Python爬虫】利用Selenium等待Ajax加载及模拟自动翻页,爬取东方财富网公司公告
- python爬虫由浅入深9---定向爬取股票数据信息并保存至本地文件
- python3.x爬虫学习:股票数据定向爬虫笔记
- printf()格式化输出详解
- 决策树学习
- 控制器controller与指令中的link、controller中同名变量作用域的关系
- 二进制位运算(与、或、异或、取反)
- InputMethodManager造成的内存泄漏问题及解决方法
- python 东方财富网&百度股票数据定向爬虫 实例
- eclipse开发springboot项目插件
- Python 返回函数 闭包
- visual studio动态库调用
- 字符串列表元组之间的转换,字符串的方法
- phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接.您应该检查配置文件中的
- 遗传算法的C语言实现(二)
- Python 装饰器
- 1108. Finding Average (20)[字符串处理]