作家
登录

Python爬虫实战:股票数据定向爬虫

作者: 来源: 2017-08-23 09:13:08 阅读 我要评论

as f: 
  •  
  • f.write( str(infoDict) + '\n' )  
  • 将上述过程封装成完成的函数,代码如下:

    1. def getStockInfo(lst, stockURL, fpath): 
    2.  
    3.     for stock in lst: 
    4.  
    5.         url = stockURL + stock + ".html" 
    6.  
    7.         html = getHTMLText(url) 
    8.  
    9.         try: 
    10.  
    11.             if html==""
    12.  
    13.                 continue 
    14.  
    15.             infoDict = {} 
    16.  
    17.             soup = BeautifulSoup(html, 'html.parser'
    18.  
    19.             stockInfo = soup.find('div',attrs={'class':'stock-bets'}) 
    20.  
    21.             name = stockInfo.find_all(attrs={'class':'bets-name'})[0] 
    22.  
    23.             infoDict.update({'股票名称'name.text.split()[0]}) 
    24.  
    25.              
    26.  
    27.             keyList = stockInfo.find_all('dt'
    28.  
    29.             valueList = stockInfo.find_all('dd'
    30.  
    31.             for i in range(len(keyList)): 
    32.  
    33.                 key = keyList[i].text 
    34.  
    35.                 val = valueList[i].text 
    36.  
    37.                 infoDict[key] = val 
    38.  
    39.              
    40.  
    41.             with open(fpath, 'a', encoding=

        推荐阅读

        浅谈JavaScript中的接口实现

      接口是面向对象JavaScript法度榜样员的对象箱中最有效的对象之一。在设计模式中提出的可重用的面向对象设计的原则之一就是“针对接口编程而不是实现编程”,即我们所说的面向接口编程,这个>>>详细阅读


      本文标题:Python爬虫实战:股票数据定向爬虫

      地址:http://www.17bianji.com/lsqh/36863.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)