鱼C论坛

 找回密码
 立即注册
查看: 1818|回复: 3

[技术交流] 爬取取票信息,怎么说?

[复制链接]
发表于 2017-12-9 15:52:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding: utf-8 -*-

  2. import requests
  3. from bs4 import BeautifulSoup
  4. import traceback
  5. import re


  6. def getHTMLText(url, code= 'utf-8'):
  7.     try:
  8.         r = requests.get(url, timeout = 30)
  9.         r.raise_for_status()
  10.         r.encoding = code
  11.         return r.text
  12.     except:
  13.         return ""
  14.    
  15. def getStockList(lst, stockURL):
  16.    
  17.     html = getHTMLText(stockURL, 'GB2312')
  18.     soup = BeautifulSoup(html, 'html.parser')#解析页面
  19.     a = soup.find_all('a')#找到<a>标签里面的信息
  20.     for i in a :
  21.         try:
  22.             href = i.attrs['href']
  23.             lst.append(re.findall(r"[s][hz]\d{6}", href)[0]) # 找到股票代码
  24.         except:
  25.             continue        

  26. def getStockInfo(lst, stockURL, fpath):
  27.     count = 0
  28.     for stock in lst:
  29.         url = stockURL + stock + ".html"#得到每个股票的链接
  30.         html = getHTMLText(url)
  31.         try:
  32.             if html == "":
  33.                 continue
  34.             infoDict = {}
  35.             soup = BeautifulSoup(html, 'html.parser')#对相应的股票页面解析
  36.             stockInfo = soup.find('div', attrs = {'class':'stock-bets'})
  37.             
  38.             name = stockInfo.find_all(attrs = {'class':'bets-name'})[0]#获得股票代码的名称
  39.             infoDict.update({'股票名称':name.text.split()[0]})#加入字典中
  40.             
  41.             keyList = stockInfo.find_all('dt')
  42.             valueList = stockInfo.find_all('dd')
  43.             for i in range(len(keyList)):
  44.                 key = keyList[i].text
  45.                 val = valueList[i].text
  46.                 infoDict[key] = val
  47.                
  48.             with open(fpath, 'a', encoding = 'utf-8') as f:
  49.                 f.write(str(infoDict) + '\n')
  50.                 count = count + 1
  51.                 print("\r当前速度:{:.2f}%".format(count*100/len(lst)), end ="")
  52.         except:
  53.             count = count + 1
  54.             print("\r当前速度:{:.2f}%".format(count*100/len(lst)), end ="")
  55.             continue
  56.         
  57.         

  58. def main():
  59.     stock_list_url = 'http://quote.eastmoney.com/stocklist.html'
  60.     stock_info_url = 'https://gupiao.baidu.com/stock/'
  61.     output_file = 'G://anaconda_project/爬虫/baiduStockInfo.txt'
  62.     sList = []
  63.     getStockList(sList, stock_list_url)
  64.     getStockInfo(sList, stock_info_url, output_file)
  65.    
  66. main()
复制代码
QQ截图20171209155046.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-9 16:53:23 | 显示全部楼层
我觉得这种完全没有说明的帖子真应该删掉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-9 17:08:54 | 显示全部楼层
ooxx7788 发表于 2017-12-9 16:53
我觉得这种完全没有说明的帖子真应该删掉

删啊,封我号都可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-10 00:51:43 | 显示全部楼层
运行一次需要好长的时间.....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 21:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表