zoukankan      html  css  js  c++  java
  • 股票数据定向爬虫.py(亲测有效)

    import requests
    from bs4 import BeautifulSoup
    import traceback
    import re
    
    def getHTMLText(url,code='utf-8'):
        try:
            r = requests.get(url,timeout=30)
            r.encoding = code
            return r.text
        except:
            return ""
    
    def getStockList(lst,stockURL):
        html = getHTMLText(stockURL)
        soup = BeautifulSoup(html,'html.parser')
        a = soup.find_all('tr')
        for i in a:
            try:
                href = i.attrs['id']
                lst.append(re.findall(r'[tr]d{6}',href)[0])
            except:
                continue
    
    def getStockInfo(lst,stockURL,fpath):
        count = 0
        for stock in lst:
            url = stockURL + stock[1:] +".html"
            html = getHTMLText(url)
            try:
                if html == "":
                    continue
                infoDict = {}
                soup = BeautifulSoup(html,'html.parser')
                stockInfo = soup.find('div',attrs={'class':'merchandiseDetail'})
                name = stockInfo.find_all(attrs={'class':'fundDetail-tit'})[0]
                infoDict.update({'股票名称':name.text.split()[0]})
                keylist = stockInfo.find_all('dt')
                valuelist = stockInfo.find_all('dd')
                for i in range(len(keylist)):
                    key = keylist[i].text
                    print(key)
                    val = valuelist[i].text
                    infoDict[key] = val
                with open(fpath,'a',encoding='utf-8')as f:
                    f.write(str(infoDict)+'
    ')
                    count = count+1
                    print('
    当前速度:{:.2f}%'.format(count*100/len(lst)),end='')
    
            except:
                count = count + 1
                print('
    当前速度:{:.2f}%'.format(count * 100 / len(lst)), end='')
                traceback.print_exc()
                continue
    
    def main():
        stock_list_url = 'https://fund.eastmoney.com/fund.html#os_0;isall_0;ft_;pt_1'
        stock_info_url = 'https://fund.eastmoney.com/'
        output_file = 'D://桌面//BaiduStockInfo.txt'
        slist = []
        getStockList(slist,stock_list_url)
        getStockInfo(slist,stock_info_url,output_file)
    
    main()
  • 相关阅读:
    [帮助文档] [SageMath Thematic Tutorial] Chapter 10 使用Sagemath进行数值计算
    [转]dd命令、cp命令详解+dd命令、cp命令对比 delong
    MBR内容解析
    记一个编译错误:命名冲突、宏定义、头文件包含顺序
    防止STL容器迭代器失效
    日历时间
    观点
    编码规范
    《转》impress.js页面PPT
    <转>Spring Test 整合 JUnit 4 使用总结
  • 原文地址:https://www.cnblogs.com/qianmo123/p/14237347.html
Copyright © 2011-2022 走看看