股票下载代码

  1. import requests  
  2. from bs4 import BeautifulSoup  
  3. import traceback  
  4. import re  
  5. def getHTMLText(url):  
  6.     try:  
  7.         user_agent = '自己的浏览器头部信息'  
  8.         headers = {'User-Agent': user_agent}  
  9.         r = requests.get(url,headers = headers,timeout = 30)  
  10.         r.raise_for_status()  
  11.         r.encoding = r.apparent_encoding  
  12.         return r.text  
  13.     except:   
  14.         return ""  
  15.   
  16. def getStockList(lst,stock_list_url):  
  17.     html = getHTMLText(stock_list_url)  
  18.     soup = BeautifulSoup(html,'html.parser')  
  19.     a = soup.find_all('a')  
  20.     for i in a:  
  21.         try:  
  22.             href = i.attrs['href']  
  23.             lst.append(re.findall(r"sh\d{6}",href)[0])  
  24.             #print(lst)  
  25.         except:  
  26.             continue  
  27.   
  28. def getStockInfo(lst,stock_info_url,fpath):  
  29.     for stock in lst:  
  30.         url = stock_info_url + stock + '.html'  
  31.         html = getHTMLText(url)  
  32.         try:  
  33.             if html =="":  
  34.                 continue  
  35.             infoDict = { }  
  36.             soup = BeautifulSoup(html,'html.parser')  
  37.             stockInfo = soup.find('div',attrs = {'class':'stock-bets'})  
  38.             if stockInfo == None:  
  39.                 continue  
  40.             #print(stockInfo)  
  41.             name = stockInfo.find_all(attrs={'class':'bets-name'})[0]  
  42.             #print(name)  
  43.             infoDict.update({'股票名称': name.text.split()[0]})  
  44.   
  45.             keyList = stockInfo.find_all('dt')  
  46.             valueList = stockInfo.find_all('dd')  
  47.             for i in range(len(keyList)):  
  48.               
  49.                 key = keyList[i].text  
  50.                 val = valueList[i].text  
  51.                 infoDict[key] = val  
  52.             with open(fpath,'a',encoding = 'utf-8') as f:  
  53.                 f.write(str(infoDict) + '\n')  
  54.         except:  
  55.             traceback.print_exc()  
  56.             continue  
  57.               
  58.   
  59.   
  60. def main():  
  61.     stock_list_url = 'http://quote.eastmoney.com/stocklist.html'  
  62.     stock_info_url = 'http://gupiao.baidu.com/stock/'  
  63.     output_file = 'D://Postgraduate//Python//python项目//Python网络爬虫与信息提取-中国大学MOOC//3 网络爬虫之实战//BaiduStockInfo.txt'  
  64.     slist = []  
  65.     getStockList(slist,stock_list_url)  
  66.     getStockInfo(slist,stock_info_url,output_file)  
  67.      
  68. main()  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值