主要有三个步骤:(1)从东方财富上爬虫读取到所有股票的codelist;(2)从凤凰网的api获取到某只股票历史上所有数据,开盘收盘价,成交量,成交金额,ma均线价格等数据;(3)通过pymysql将获取到的数据存储到本地。
第一个步骤的实现,从EAST_MONEY_URL = 'http://quote.eastmoney.com/stocklist.html'处获取stocklist。主要使用
#encoding = uft-8
import requests
from bs4 import BeautifulSoup
import re
class GetStockList(object):
def get_html_text(self, url):
try:
response = requests.get(url)
response.raise_for_status()
response.encoding = 'utf-8'
return response.text#html变量存储读取的网页text
except:
return ''
def get_stock_list(self, url):
html = self.get_html_text(url)
soup = BeautifulSoup(html, 'html.parser')
a = soup.find_all('a')
stock_list = []
for i in a: