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()
  • 相关阅读:
    javascript推荐书籍
    关于Cookie和Session的优缺点
    PHP try catch
    DQL、DML、DDL、DCL的概念与区别
    XP/Win7下QTP11循环试用30天的破解方法
    struts.xml配置详解
    MyEclipse8.5破解方法
    Myeclipse编写struts程序
    关于Hibernate的关联映射
    Java代码到字节码——第一部分
  • 原文地址:https://www.cnblogs.com/qianmo123/p/14237347.html
Copyright © 2011-2022 走看看