功能描述:获取上交所和深交所所有股票名称和交易信息
输出:保存到文本文件中
技术路线:requests - bs4 - re
数据网站的确定:
获取股票列表 - 东方财富网:http://quote.eastmoney.com/stock_list.html
获取个股信息 - 百度股票:https://gupiao.baidu.com/stock/ (例:https://gupiao.baidu.com/stock/sz000100.html
import requests
from bs4 import BeautifulSoup
import re
def 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)
soup = BeautifulSoup(html, 'html.parser')
a = soup.find_all('a')
for i in a:
try:
href =