看准网A股评论数据获取

获取看准网A股评论数据,部分数据需要登录才能获取,需要给浏览器加上代理。

# -*- coding: utf-8 -*-

"""================================================================================================================
@date     :  2022/11/19  17:06
@function :  看准网
            股票代码    公司全称    搜索到的公司  点评人数1   点评人数2   总得分 薪酬福利总得分 职业发展总得分 公司认同总得分 工作条件总得分 工作生活平衡总得分   用户类型:匿名用户等  任职/在职(工作过)那块    评论内容    评论时间    评论总得分   评论总得分   评论薪酬福利得分    评论薪酬福利得分    评论职业发展得分    评论职业发展得分    评论公司认同得分    评论公司认同得分    评论工作条件得分    评论工作条件得分    评论工作生活平衡得分  评论工作生活平衡得分
================================================================================================================"""

import re
import csv
import time
import random
import pandas as pd
from bs4 import BeautifulSoup
from loguru import logger
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from concurrent.futures import ThreadPoolExecutor

from utils.request import Request

from get_company_name import KanZhunSeleniumNoLogin


class KanZhun:
    def __init__(self):
        self.selenium_no_login = KanZhunSeleniumNoLogin()

        self.headers = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                        'accept-encoding': 'gzip, deflate, br',
                        'accept-language': 'zh-CN,zh;q=0.9',
                        'cache-control': 'max-age=0',
                        # 'cookie': 'wd_guid=f803fd1a-c3d3-4e11-b92f-d95d4dc9ca37; W_CITY_S_V=50; __t=nG8GtfU74cmValk; ac=18512143810; historyState=state; __lh_v1=4_131286; AB_T=abva; __g=-; Hm_lvt_1f6f005d03f3c4d854faec87a0bee48e=1668955744,1668957607,1669035462,1669188029; __c=1669191789; __l=r=https%3A%2F%2Fwww.kanzhun.com%2Ffirm%2Freview%2F0nJ839q-Fg~~%2F&l=%2Fqrcode%2FcreateQrCode%3FaccountId%3D9%26page%3Dmodules%2Fcompany%2Fpages%2Fcompany%2Fcompany%26param%3Dc%3D5664736%26s%3D1%26n%3D10; JSESSIONID=""; R_SCH_CY_V=899085|25055|1800226|31558|2091844; __a=16527091.1668955743.1669188029.1669191789.2652.6.177.2652; t=nG8GtfU74cmValk; Hm_lpvt_1f6f005d03f3c4d854faec87a0bee48e=1669293547',
                        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
                        }

        self.rq = Request()
        option = Options()
        option.add_experimental_option('debuggerAddress', '127.0.0.1:9202')
        self.driver = webdriver.Chrome(options=option)
        self.wait = WebDriverWait(self.driver, 60)
        self.score_pattern = re.compile('综合评分(.*?)分:薪酬福利怎么样:(.*?)分、工作条件怎么样:(.*?)分、工作生活平衡怎么样:(.*?)分、职业发展怎么样:(.*?)分、公司认同度怎么样:(.*?)分。')
        csv_f = open('result1.csv', 'a+', newline='', encoding='utf-8')
        self.csv_writer = csv.writer(csv_f)
        # header = ['股票代码','公司全称','搜索到的公司','点评人数1','点评人数2','总得分','薪酬福利总得分','职业发展总得分','公司认同总得分',
        #           '工作条件总得分','工作生活平衡总得分','用户类型:匿名用户等','任职/在职','评论内容','评论时间','评论总得分','评论总得分',
        #           '评论薪酬福利得分','评论薪酬福利得分','评论职业发展得分','评论职业发展得分','评论公司认同得分','评论公司认同得分',
        #           '评论工作条件得分','评论工作条件得分','评论工作生活平衡得分','评论工作生活平衡得分']
        # self.csv_writer.writerow(header)

    def add_to_csv(self, datas):
        self.csv_writer.writerows(datas)

    def get_company(self):
        df = pd.read_excel('companys.xlsx')
        companys = df.values.tolist()
        print(len(companys), companys[:3])
        return companys

    def parse_star(self, word):
        return 1 if word in ['很差', '很不满意', '很不认同'] else 2 if word in ['差', '不满意', '不认同'] else 3 if word in ['一般'] else 4 if word in ['很棒', '好', '满意', '认同'] \
            else 5 if word in ['完美!', '非常好', '满意', '非常满意', '非常认同'] else ''

    def parse_exception(self, obj):
        rs = obj.result()
        if rs:
            print('error', rs)

    def treat_one_comment(self, i, l, company_code, comment_code):
        """单一评论详情页,不需要登录"""
        logger.info(f'{i}/{l} {company_code} {comment_code}')
        url = f'https://www.kanzhun.com/firm/review/{company_code}/{comment_code}.html'
        print('comment_url', url)
        z = 0
        while z < 5:
            response = self.rq.requests_get(url, headers=self.headers)
            if type(response) == int:
                z += 1
                print(f'try {z} times:{url}')
            else:
                # print(type(response))
                break
        else:
            raise
        soup = BeautifulSoup(response.text, 'lxml')
        username = soup.select('.comment-detail .user span')[0].text.strip()
        status = soup.select('.user-info')[0].text.replace(username, '').strip()
        content = soup.select('.content-wrap .text-content')[0].text.replace('\n', ' ').replace('\r', ' ').replace('\u200b', ' ').replace('\xa0', '').strip()
        time_str = soup.select('.publish span')[0].text.strip()
        total_score_text = soup.select('.comment-box .top .kz-rate-text')[0].text.strip()
        total_score = self.parse_star(total_score_text)
        score1_text = soup.select('.comment-box .center .kz-rate-text')[0].text.strip()
        score1 = self.parse_star(score1_text)
        score2_text = soup.select('.comment-box .center .kz-rate-text')[1].text.strip()
        score2 = self.parse_star(score2_text)
        score3_text = soup.select('.comment-box .center .kz-rate-text')[2].text.strip()
        score3 = self.parse_star(score3_text)
        score4_text = soup.select('.comment-box .center .kz-rate-text')[3].text.strip()
        score4 = self.parse_star(score4_text)
        score5_text = soup.select('.comment-box .center .kz-rate-text')[4].text.strip()
        score5 = self.parse_star(score5_text)
        data = [username, status, content, time_str, total_score_text, total_score, score1_text, score1, score2_text, score2,
                score3_text, score3, score4_text, score4, score5_text, score5]
        print(data)
        self.data.append(data)
        # return data

    def comment_page(self, company_code, id, name):
        """评论页,需要登录"""
        comment_url = f'https://www.kanzhun.com/firm/review/{company_code}/'
        print('comment_url', comment_url)
        self.driver.get(comment_url)
        time.sleep(3)
        if '暂无公司点评' in self.driver.page_source or '如果你在这里工作过,欢迎分享工作体验' in self.driver.page_source:
            print('暂无公司点评')
            self.add_to_csv([[id, name, name]])
            return
        if '哎呀,我们没办法找到这个页面了' in self.driver.page_source:
            print('哎呀,我们没办法找到这个页面了')
            raise
            # self.add_to_csv([[id, name, name]])
            # return
        total_comment_num1 = self.driver.find_element(By.CSS_SELECTOR, '.page-tab .number').text.strip()
        try:
            total_comment_num2 = self.driver.find_element(By.CSS_SELECTOR, '#list .title-content .number').text.strip()
        except:
            total_comment_num2 = 0
            print('暂无公司点评')
            self.add_to_csv([[id, name, name]])
            return
        print('total_comment_num1', total_comment_num1, 'total_comment_num2', total_comment_num2)
        # 单项评分
        description = self.driver.find_element(By.CSS_SELECTOR, '[name="description"]').get_attribute('content')
        try:
            scores = re.search(self.score_pattern, description).groups()
        except AttributeError:
            scores = ('', '', '', '', '', '')
        print(scores)

        i = 0
        flag = 0   # [连续] 5次不增长则刷新
        while i < 80:
            print('i', i)
            js = 'window.scrollTo(0, 1000000)'
            self.driver.execute_script(js)
            time.sleep(2)
            js = "return document.documentElement.scrollHeight"
            height = self.driver.execute_script(js)
            js = f'window.scrollTo(0, {height * random.choice([0.5,0.6,0.7,0.8])})'
            self.driver.execute_script(js)
            time.sleep(2)
            js = f'window.scrollTo(0, {height * random.choice([0.5,0.6,0.7,0.8])})'
            self.driver.execute_script(js)
            time.sleep(2)
            comments = self.driver.find_elements(By.CSS_SELECTOR, '.bala-list_VWnqh .kz-bala-card_d73dq')
            print('comments', len(comments))
            if len(comments) == 10:
                flag += 1
                if flag == 5:
                    self.driver.refresh()
                    print('flag=5, refresh')
                    flag = 0
                    continue
            if '没有更多了' in self.driver.page_source:
                print('没有更多了')
                break
            else:
                i += 1
                continue
        else:
            raise Exception
        comment_codes = [re.search("review/.*?/(.*?).html", i.find_element(By.CSS_SELECTOR, '.header a').get_attribute('href')).group(1) for i in comments]
        self.data = []
        exec = ThreadPoolExecutor(max_workers=5)
        for index, comment_code in enumerate(comment_codes):
            # self.treat_one_comment(index, len(comments), company_code, comment_code)
            exec.submit(self.treat_one_comment, index, len(comments), company_code, comment_code).add_done_callback(self.parse_exception)
        exec.shutdown(wait=True)
        self.data = [[id, name, name, total_comment_num1, total_comment_num2] + list(scores) + i for i in self.data]
        print('data', len(self.data))
        self.add_to_csv(self.data)

    def search_one(self, company):
        """搜索公司名称,取第一个,这里无需登录"""
        id, name = company[0], company[1]
        id = '%06d' % id
        print('id', id)
        # name = '中国南玻集团股份有限公司'
        try:
            first_company_code = self.selenium_no_login.search_one(name)
        except:
            first_company_code = self.selenium_no_login.search_one(name)
        if not first_company_code:
            print('暂无公司')
            self.add_to_csv([[id, name, '']])
            return
        try:
            self.comment_page(first_company_code, id, name)
        except:
            # 重新点击登录
            print('重新点击登录')
            try:
                self.driver.find_element(By.CSS_SELECTOR, '.btn-login').click()
                time.sleep(3)
            except:
                pass
            try:
                self.comment_page(first_company_code, id, name)
            except:
                time.sleep(6)
                self.comment_page(first_company_code, id, name)

    def main(self):
        companys = self.get_company()
        # companys = [('002695','江西煌上煌集团食品股份有限公司')]
        for index, company in enumerate(companys):
            if index < 0:
                continue
            logger.info(f'{index}/{len(companys)} {company}')
            self.search_one(company)


if __name__ == '__main__':
    c = KanZhun()
    c.main()
    # c.search_one()
    # c.comment_page('1Xd62dw~', '000012', '中国南玻集团股份有限公司')

get_company_name.py

# -*- coding: utf-8 -*-

"""================================================================================================================
@date     :  2022/11/20  9:26
@function :  看准网 搜索公司名称 这里处理不需要登录的部分
================================================================================================================"""


import re
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

from utils.proxy import selenium_proxy_xiaoxiang


class KanZhunSeleniumNoLogin:
    def __init__(self):
        self.driver = selenium_proxy_xiaoxiang()
        self.wait = WebDriverWait(self.driver, 30)
        self.score_pattern = re.compile('综合评分(.*?)分:薪酬福利怎么样:(.*?)分、工作条件怎么样:(.*?)分、工作生活平衡怎么样:(.*?)分、职业发展怎么样:(.*?)分、公司认同度怎么样:(.*?)分。')

    def search_one(self, name):
        """搜索公司名称,取第一个,这里无需登录"""
        url = f'https://www.kanzhun.com/search?pageNum=1&query={name}&type=1'
        x = 0
        while x < 5:
            try:
                self.driver.get(url)
                if '没有找到相关结果' in self.driver.page_source:
                    print('没有找到相关结果')
                    return
                self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.search-title')))
                break
            except:
                x += 1
                print(f'try {x} times')
        else:
            raise
        first_company_name = self.driver.find_element(By.CSS_SELECTOR, '.company-list .company-info .middle a[href*="firm/info"]').get_attribute('title').replace('(', '(').replace(')', ')')
        first_company_code = self.driver.find_element(By.CSS_SELECTOR, '.company-list .company-info .middle a[href*="firm/info"]').get_attribute('href').split('/info/')[-1].split('.')[0]
        print('first_company_name', first_company_name, 'first_company_code', first_company_code)
        if first_company_name == name:
            return first_company_code
        else:
            print(f'no result:{first_company_name}')
            return None


if __name__ == '__main__':
    c = KanZhunSeleniumNoLogin()
    # c.comment_page()

欢迎关注:爬虫王者

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容