爬虫笔记——东方财富科创板数据爬取(selenium方法)

爬虫笔记——东方财富科创板数据爬取(selenium方法)

  • 网站观察
  • 网站分析
    • 公司详情页面
  • 具体代码

selenium方式爬取
优点:无需观察网站去查找数据来源
缺点:速度较requests方法更慢

网站观察

网址:东方财富科创板数据

===========================================================
爬虫笔记——东方财富科创板数据爬取(selenium方法)_第1张图片

===========================================================

网站分析

可以发现,由于在网页中存在多个公司,且网站代码中对每个标签的区分度并不高,所以直接在该网页中爬取比较复杂,所以这里选择先爬取每个公司的详情介绍的链接(href属性中每个公司都有一个对应的code编号),然后在每个公司的详情链接网站中爬取想要的信息。
爬虫笔记——东方财富科创板数据爬取(selenium方法)_第2张图片

===========================================================

公司详情页面

公司详情链接的页面如下:
爬虫笔记——东方财富科创板数据爬取(selenium方法)_第3张图片

===========================================================
可以发现网站也是动态加载的,那么可以采取selenium或者requests的方式爬取数据,其中selenium方式不需要去查找动态加载部分,直接爬取就行,但是由于每次需要访问不同页面,速度就要更慢,这里介绍selenium方法爬取,下一篇文章:爬虫笔记——东方财富科创板数据爬取(requests方法)介绍requests方法。

具体代码

代码如下,注意在run函数中,需要根据爬取的总共条数定义一下函数的参数,这里由于每页为50个公司,总共只有149条数据,故参数为(50,149):

# 东方财富科创板数据爬取-selenium
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from lxml import etree
import re
import time
import pandas as pd

class eastmoneyspider():
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.url = "http://data.eastmoney.com/kcb/"
        self.page = 1
        self.company_detail_urls = []
        self.kcb_data = []
        self.spider_count = 1
    
    def run(self):
        # 爬取公司详细网址(这里共有149个公司,每页50条数据,第三页只有49条,可根据变动情况更改函数参数)
        self.parse_detail_url(50,149)
        # 详细数据爬取
        self.parse_page()
        # 数据保存
        self.save_data()
        print("爬取完成!")
    
    # 获取每个公司的链接,其中pagesize为每页公司个数,datasize为公司总数
    def parse_detail_url(self,pagesize,datasize):
        self.driver.get(self.url)
        while True:
            # 下一页按钮
            nextpage_Btn = self.driver.find_element_by_xpath('//div[@id="PageCont1"]/a[last()-1]')
            print("即将爬取第%d页"%self.page)
            # 爬取过程
            WebDriverWait(self.driver,timeout=10).until(EC.presence_of_element_located((By.XPATH,'//div[@id="PageCont1"]/a[last()-1]')))
            source = self.driver.page_source
            detail = re.findall('/kcb/detail/(.*?).html',source)
            # 每个页面中存在重复数据,下面去除重复部分
            urls = list(set(detail))
            urls.sort(key=detail.index)
            # 将网址填全
            detail_urls = list(map(lambda x:"http://data.eastmoney.com/kcb/detail/"+x+".html",urls))
            # 由于在每个页面中存在其他公司数据(拟申报企业),这里只取每页的前pagesize(50)个,即每页显示的个数
            detail_urls = detail_urls[:pagesize]
            # 公司详情网址汇总
            [self.company_detail_urls.append(x) for x in detail_urls]
            
            print("第%d页爬取完成"%self.page)
            
            if "nolink" in nextpage_Btn.get_attribute("class"):
                self.company_detail_urls = self.company_detail_urls[:datasize]
                print("公司详情网址爬取完成!")
                print("="*40)
                break
            else:
                nextpage_Btn.click()
                self.page += 1
                time.sleep(1)
    
    # 获取每个公司的详细信息
    def parse_page(self):
        print("开始爬取公司详细数据")
        for url in self.company_detail_urls:
            self.driver.get(url)
            source = self.driver.page_source
            html = etree.HTML(source)
            
            company_details = html.xpath('//tr/td/text()')
            company_name = company_details[2]
            company_abbreviation = company_details[6]
            accept_date = company_details[4]
            update_date = company_details[12]
            state = company_details[10]
            registration = company_details[14]
            industry = company_details[16]
            sponsorship_agency = company_details[18]
            law_agency = company_details[26]
            account_agency = company_details[22]
            
            company = {
                    "公司名称":company_name,
                    "公司简称":company_abbreviation,
                    "公司详情网址":url,
                    "受理日期":accept_date,
                    "更新日期":update_date,
                    "审核状态":state,
                    "注册地":registration,
                    "行业":industry,
                    "保荐机构":sponsorship_agency,
                    "律师事务所":law_agency,
                    "会计师事务所":account_agency
                    }
            
            self.kcb_data.append(company)
            print("已爬取%d条数据"%self.spider_count)
            self.spider_count += 1
    
    def save_data(self):
        data = pd.DataFrame(self.kcb_data)
        data = data[["公司名称","公司简称","公司详情网址","审核状态","注册地","行业","保荐机构","律师事务所","会计师事务所","更新日期","受理日期"]]
        data.to_excel('./data/kcb_data_spider_selenium.xlsx',encoding='utf-8-sig',index=False)

def main():
    kcb_data = eastmoneyspider()
    kcb_data.run()
    
main()

有用就点个赞吧!

你可能感兴趣的:(python爬虫笔记)