Python股票数据爬虫

最近再看python的爬虫, 刚好有人问到能不能把所有的股票数据爬下来. 看一些其他人的实现方式,可能一些网站进行了优化,竟然没有找到能完全直接用的, 但得到了很好的思路. 简单记录一下,方便以后自己使用.

准备工作.

安装anaconda. 使用jupyter.

安装BeautifulSoup, requests 包

实现步骤:

1. 得到股票列表

def is_stock(href_para):
    if href_para is not None:
        href_list = href_para.split('/')
        last = href_list[-1]
#         print(last)

#判断是上海或者深圳的股票,因为里面还有一些其它,比如基金.
        ret= re.match("^[s][hz][0,3,6]\d{5}\.html$", last) 
#         print(ret)
        return ret


f =open('stock.csv','w+',encoding='utf-8',newline="")
writer = csv.writer(f)
# writer.writerow(('代码', '名称', '链接'))

stock_data = soup.find_all("a", href=is_stock)
# print(stock_data)

#储存到一个list,当然也把它存储到一个文件,方便以后使用

stock_list = []
for stock_it in stock_data:
    print(stock_it)
    num = re.findall("\d{6}",stock_it.contents[0])
    

你可能感兴趣的:(技术,总结,stock,python)