主要参考了北京理工大学嵩天老师的视频,因老师所讲的网址已做更改,将获取股票列表信息和股票价格的网站做了更改,用到了beautiful soup库,re库,requests库,traceback库,datetime库。
环境说明:
Python:3.8(32bit)
IDE:Pycharm
OS:win10
访问网页数:996
运行时间:约600s
程序运行截图:
本地文件保存部分效果图:
股票列表网页源码:
<div class="u-postcontent cz" id="ctrlfscont">
<ul>
<li><a href="/gupiao/600010/">包钢股份(600010)</a></li>
网页信息源码:
<ul class="clear" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0"><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.0"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.0.0">昨 收:</span><span class="col-2-1 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.0.1">1145.00</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.1"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.1.0">今 开:</span><span class="col-2-2 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.1.1">1139.03</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.2"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.2.0">最 高:</span><span class="col-2-3 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.2.1">1147.96</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.3"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.3.0">最 低:</span><span class="col-2-4 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.0.3.1">1131.98</span></li></ul><ul class="clear" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1"><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.0"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.0.0">成交量:</span><span class="col-2-1 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.0.1">27618手</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.1"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.1.0">成交额:</span><span class="col-2-2 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.1.1">31.48亿</span></li><li style="display:block;background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.2"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.2.0">总市值:</span><span class="col-2-3 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.2.1">14千亿</span></li><li style="display:block;background-color:#fff;" title="流通市值:可交易的流通股股数乘以当时股价得出的流通股票总价值" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.3"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.3.0">流通市值:</span><span id="main-11" class="col-2-4 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.3.1">14千亿</span></li><li style="display:none;background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.4"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.4.0">振 幅:</span><span class="col-2-4 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.1.4.1">1.40%</span></li></ul><ul style="display:block;" class="clear" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2"><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.0"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.0.0">换手率:</span><span class="col-2-1 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.0.1">0.22%</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.1"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.1.0">市净率:</span><span class="col-2-2 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.1.1">11.43</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.2"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.2.0">振 幅:</span><span class="col-2-3 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.2.1">1.40%</span></li><li style="background-color:#fff;" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.3"><span data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.3.0">市盈率:</span><span class="col-2-4 bl" data-reactid=".evilk4e1gq.0.4.1.0.0.0.0.1.1.2.3.1">34.99</span></li></ul>
源码如下:
import re
import requests as res
from bs4 import BeautifulSoup as bs
import traceback
import datetime as dt
def getHTMLText(url,code="utf-8"):
try:
r=res.get(url,timeout=30)
r.raise_for_status()
r.encoding=code
return r.text
except:
return ""
def getStockList(lst,stockURL):
r = getHTMLText(stockURL)
soup = bs(r,"html.parser")
try:
for a in soup.find_all("div",id="ctrlfscont"):
for b in a.find_all("a"):
match = re.split(r"\(",b.string)
if match:
info = {match[0]:[match[1].split(")")[0]]}
lst.append(info)
except:
traceback.print_exc()
def getStockInfo(lst,stockURL,fpath):
count = 0
u1 = stockURL.split(",")[0]
u2 = stockURL.split(",")[1]
try:
for i in range(len(lst)):
for key, value in lst[i].items():
newURL = u1 + value[0] + u2
r = getHTMLText(newURL)
soup = bs(r, "html.parser")
for a in soup.find_all("ul",attrs={"class":"clear"}):
for b in a.find_all("span"):
lst[i][key].append(b.string)
with open(fpath,"a",encoding="utf-8") as f:
f.write("时间:"+str(dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))+"\n")
f.write(str(key)+"\n"+"股票代码: ")
for c in range(len(lst[i][key])):
f.write(lst[i][key][c]+" ")
f.write("\n\n")
count = count + 1
print("\r当前速度:{:.2f}%".format(count * 100 / len(lst)), end="")
f.close()
except:
count = count + 1
print("\r当前速度:{:.2f}%".format(count * 100 / len(lst)), end="")
traceback.print_exc()
def main():
stock_list_url = "https://www.banban.cn/gupiao/list_sh.html"#股票列表网址
stock_info_url = "http://gu.qq.com/sh"+","+"/gp"#股票信息网址
output_file = "D:/1.txt"#要写入的本地文件地址
slist = []
getStockList(slist,stock_list_url)
getStockInfo(slist,stock_info_url,output_file)
main()
请多指教!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)