东方财富网上市公司概况信息采集及分析过程

669 阅读3分钟

1.声明:

本项目的所有代码和相关文章, 仅用于经验技术交流分享,禁止将相关技术应用到不正当途径,因为滥用技术产生的风险与本人无关。

2.目标

根据企业股票代码来采集东方财富网上市企业的公司概况信息。

​编辑

 3.分析步骤

1.首先来到公司概况详情页(以平安银行为例)。

​编辑

2.通过F12抓包分析。

​编辑

很容易就找到该企业公司概况的数据包,接下来对该接口的请求参数进行分析。

3.请求参数

​编辑

发现很简单,只有一个code的参数,但是问题来了,我们只有一个公司股票代码(000001),那我们怎么去确定他的前缀“sz”?经过抓包几个不同的企业发现他们的前缀是不同的,sz代表深圳。。。。。。。

4.去获取股票前缀

​编辑

​编辑

经过多次分析与调试,找到搜索栏,在搜索栏内输入股票代码000001,然后打开元素面板找到行情的位置(见上图),其中拿到"/unify/r/0.000001"对其的链接进行请求,发现会自动重定向到"/sz000001.html" ,如下图所示

​编辑

仔细观察,发现通过该方法可以帮助我们确定前面分析的详情页接口中code参数的代码前缀”sz“。我们回头看看刚才我们请求的url"/unify/r/0.000001",发现并不是简单通过股票代码”000001“去请求的,而是在”000001“前有一个”0.“,那问题又来了,“0.”是怎么来的呢?接下来,我们继续去分析”0.“的由来。

5.分析”unify/r/0.000001“中”0.“的由来

​编辑

我们来到东方财富的首页,在搜索栏中输入”000001“,然后抓包找到上图的接口,来到Previwe,发现QuoteID便是我们要找的东西,至此整个项目的分析工作到此完成,下面直接上代码

4.上代码

# -*- coding: utf-8 -*-
import requests
import parsel
import json
from jsonpath import jsonpath
import time
​
def init_request(code):
    headers = {
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Referer': 'https://www.eastmoney.com/',
        'Sec-Fetch-Dest': 'script',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }
    params = {
        'input': code,
        'type': '14',
    }
    response = requests.get('https://searchapi.eastmoney.com/****/get', params=params,
                            headers=headers).json()
    QuoteID = response['QuotationCodeTable']['Data'][0]['QuoteID']
​
    response = requests.get(f'http://quote.eastmoney.com/***/{QuoteID}').text
    selector =parsel.Selector(response)
    overview_url = selector.xpath('//a[text()="公司概况"]/@href').get()
    end_code = overview_url.split('=')[-1]
​
    return end_code
​
def request_url(code):
    headers = {
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    params = {
        'code': f'{code}',
    }
    response = requests.get('http://emweb.securities.eastmoney.com/******/PageAjax', params=params,headers=headers, verify=False).json()
    return response
​
def parse_response(response):
    item = {}
    item['companyName'] = jsonpath(response, "$..ORG_NAME")[0]  # 公司名称
    item['numberOfEmployees'] = jsonpath(response, "$..EMP_NUM")[0]  # 雇员人数
    item['companyProfile'] = jsonpath(response, "$..ORG_PROFILE")[0].strip()  # 公司简介
    item['sfcIndustry'] = jsonpath(response, "$..INDUSTRYCSRC1")[0]  # 所属证监会行业
    item['dateOfEstablishment'] = jsonpath(response, "$..FOUND_DATE")[0]  # 成立日期
    return item
​
​
​
if __name__ == '__main__':
    code_list = [
        "3002**",
        "6880**",
        "3003**",
        "6030**",
        "3002**",
        "6882**",
        "3000**",
        "3001**",
        "6002**",
        "0023**",
        "3004**",
        "6019**",
        "8362**",
        "6882**",
        "3012**",]
    start = time.time()
    for code in code_list:
        init_code = init_request(code)
        response = request_url(init_code)
        item = parse_response(response)
        print(item)
    end = time.time()
    print(f'总耗费时间:{end-start}')

5.程序运行效果图

​编辑

 项目至此已全部完成,但此项目为单线程爬虫方式,通过打印执行时间发现耗时29多秒,这个速度着实有点拉跨,接下来笔者会在这项目的基础上进行各种多任务的爬虫方式进一步优化。

6.下集预告:多任务加快程序采集速度

以上均为学习分享,切勿用到不正当途径,欢迎留言交流!