博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用python爬虫爬取股票数据
阅读量:4554 次
发布时间:2019-06-08

本文共 2758 字,大约阅读时间需要 9 分钟。

前言:

编写一个爬虫脚本,用于爬取东方财富网的上海股票代码,并通过爬取百度股票的单个股票数据,将所有上海股票数据爬取下来并保存到本地文件中

系统环境:

64位win10系统,64位python3.6,IDE位pycharm

预备知识:

BeautifulSoup的基本知识,re正则表达式的基本知识

代码:

import requestsfrom bs4 import BeautifulSoupimport tracebackimport redef getHTMLText(url):    try:        user_agent = '自己的浏览器头部信息'        headers = {
'User-Agent': user_agent} r = requests.get(url,headers = headers,timeout = 30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return ""def getStockList(lst,stock_list_url): html = getHTMLText(stock_list_url) soup = BeautifulSoup(html,'html.parser') a = soup.find_all('a') for i in a: try: href = i.attrs['href'] lst.append(re.findall(r"sh\d{6}",href)[0]) #print(lst) except: continuedef getStockInfo(lst,stock_info_url,fpath): for stock in lst: url = stock_info_url + stock + '.html' html = getHTMLText(url) try: if html =="": continue infoDict = { } soup = BeautifulSoup(html,'html.parser') stockInfo = soup.find('div',attrs = {
'class':'stock-bets'}) if stockInfo == None: continue #print(stockInfo) name = stockInfo.find_all(attrs={
'class':'bets-name'})[0] #print(name) 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 = 'http://gupiao.baidu.com/stock/' output_file = 'D://Postgraduate//Python//python项目//Python网络爬虫与信息提取-中国大学MOOC//3 网络爬虫之实战//BaiduStockInfo.txt' slist = [] getStockList(slist,stock_list_url) getStockInfo(slist,stock_info_url,output_file)main()

代码解释:

第一个getHTMLText函数的作用是获得所需的网页源代码
第二个getStockList函数的作用是获得东方财富网上面上海股票的全部代码,查看网页源代码可知,股票代码的数据放在'a'标签里面,如下图所示: 因此,首先用find_all方法遍历所有'a'标签,然后在'a'标签里面提取出href部分信息,在提取出来的href信息里面,用正则表达式匹配所需的信息,“sh\d{6}”,即徐亚匹配例如sh200010的信息 第三个函数需要根据第二个函数得到的股票代码,拼接出一个url,在这个特定的url的网页里,使用第一个函数解析网页,首先加一个判断,如果遇到html为空,那么要继续执行下去,同样,我们也需要再加一个判断(关键之处),遇到网页不存在, 但html源代码仍然是存在的,因此接下去这个命令
stockInfo = soup.find('div',attrs = {
'class':'stock-bets'})

可能为空,如果不加判断,程序执行到这里就会报错而无法继续执行,因此添加:

if stockInfo == None:    continue

 

 

转载于:https://www.cnblogs.com/yqpy/p/7806085.html

你可能感兴趣的文章
WPF MVVM框架PRISM
查看>>
UVa 208 - Firetruck <双向DFS>
查看>>
winform treeview绑定数据 DOM操作
查看>>
linux centos oracle11g 单机完整部署分享
查看>>
hznu
查看>>
《JAVA与模式》之模板方法模式 (转)
查看>>
JavaSE:八种基本数据类型
查看>>
JumpServer 堡垒机 指南
查看>>
IOS UIApplication和AppDelegate 关系
查看>>
HTML5新表单元素
查看>>
Linux CentOS下Python+robot framework环境搭建
查看>>
36: Same Tree
查看>>
bzoj 3671: [Noi2014]随机数生成器【模拟+贪心】
查看>>
MVC html.Telerik 开元控件在线文档
查看>>
题目1545:奇怪的连通图
查看>>
冲刺周期第九天
查看>>
C#操作SQL Server数据库
查看>>
对linux中source,fork,exec的理解以及case的 使用
查看>>
[BZOJ 1816] [Cqoi2010] 扑克牌 【二分答案】
查看>>
懒加载图片
查看>>