欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python爬虫---爬取股票信息

程序员文章站 2022-05-14 21:22:25
...

最近开了个股票账户,爬取一下300和600开头的股票信息,来筛选股票

仅仅爬取信息,不做排序和分析

代码地址

包含的库

import requests
from bs4 import BeautifulSoup
import traceback
import re

获取网页源码信息

def getHTMLText(url, code="utf-8"):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = code
        return r.text
    except:
        return ""

所有股票中选择300或者600开头的股票加入列表

def getStockList(lst, stockURL):
    html = getHTMLText(stockURL, "GB2312")
    soup = BeautifulSoup(html, 'html.parser') 
    a = soup.find_all('a')
    for i in a:
        try:
            href = i.attrs['href']
            lst.append(re.findall(r"[s][hz][36]\d{5}", href)[0])
        except:
            continue

获得股票的详细信息

这里选择了股票公司的总市值、净资产、净利润、市盈率、市净率、毛利率、净利率和ROE进行爬取,并保存到文件中

def getStockInfo(lst, stockURL, fpath):
    Listtitle=['名称','总市值','净资产','净利润','市盈率','市净率','毛利率','净利率','ROE']
    with open(fpath,'w',encoding='utf-8') as f:
        for i in range(len(Listtitle)):
            f.write("{0:<10}\t".format(Listtitle[i],chr(12288)))
    count = 0
    for stock in lst:
        url = stockURL + stock + ".html"
        html = getHTMLText(url,"GB2312")
        try:
            if html=="":
                continue
            List=[]
            soup = BeautifulSoup(html, 'html.parser')
            stock = soup.find('div',attrs={'class':'cwzb'}).find_all('tbody')[0]
            name=stock.find_all('b')[0]
            List.append(name.text)
            keyList = stock.find_all('td')[1:9]
            for i in range(len(keyList)):
                List.append(keyList[i].text)
            with open(fpath,'a',encoding='utf-8') as f:
                f.write('\n')
                for i in range(len(List)):
                    f.write('{0:<10}\t'.format(List[i],chr(12288)))
            count = count + 1
            print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="")
        except:
            count = count + 1
            print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="")
            continue

主函数调用

def main():
    stock_list_url = 'http://quote.eastmoney.com/stocklist.html'
    stock_info_url = 'http://quote.eastmoney.com/'
    output_file = './Stock.txt'
    slist=[]
    getStockList(slist, stock_list_url)
    getStockInfo(slist, stock_info_url, output_file)
相关标签: python 爬虫