项目(一)爬取公司的评论信息

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_41611045/article/details/102766989

1、首先看准网上的评论是用json处理的,所以我们要通过json抓包,其中为了要抓取各个公司的评论,我们先从首页上将各个公司的companyID和conpanyname进行获取,然后在url中限制参数。

'http://www.kanzhun.com/gsrPage.json?companyId=194222&companyName=%E4%B8%AD%E6%95%B0%E9%80%9A&pageNum=2&cityCode=&sortMethod=1&employeeStatus=0'

companyId:公司的ID编号
companyname:公司名字
cityCode:公司所在的城市
pageNum:第几页。
利用这些参数合成url网页地址。
设置页数,一般我设置100页。

import requests
from bs4 import BeautifulSoup
import pandas as pd

headers = {'Accept':'text/html, */*; q=0.01','Accept-Encoding':'gzip, deflate, sdch','Accept-Language':'zh-CN,zh;q=0.8','User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36'}
cookies = {'Cookie':'aliyungf_tc=AQAAAD2MPjQrcwQAp4x2Dgdwc71am5e9; __c=1491732911; W_CITY_S_V=57; __g=-; isHasPushRecommentMessage=true; thirtyMinutes=true; isShowDownload=false; thirtyMinutesCount=2; pageType=2; ac="[email protected]"; __t=ZPp3Vr6QMt1cLNx; __l=r=&l=%2Fgsr194222.html%3Fka%3Dpercent-review-list; __a=29429174.1491732911..1491732911.7.1.7.7; t=ZPp3Vr6QMt1cLNx; AB_T=abvb'}
url1 = 'http://www.kanzhun.com/gsrPage.json?companyId=194222&companyName=%E4%B8%AD%E6%95%B0%E9%80%9A&pageNum='
url2 = '&cityCode=&sortMethod=1&employeeStatus=0'
name2 = [] #合并name字段各列表内容
score2 = []#合并score字段各列表内容
content2 = []#合并content字段各列表内容
question2 = []
for i in range(1,8):
    url = url1 + str(2) + url2
    response = requests.get(url,headers = headers,cookies = cookies)
    soup = BeautifulSoup(response.text,'lxml')
    name = soup.find_all('p',class_='f_14 grey_99 dd_bot')
    for n in name:
        name1 = n.get_text()
        name2.append(name1)
    score = soup.find_all('span',class_='grade')
    for s in score:
        score1 = s.get_text()
        score2.append(score1)
    content = soup.find_all('h3',class_='question_title')
    for c in content:
        content1 = c.get_text()
        content11 = content1.replace('\n','')
        content2.append(content11)
    question = soup.find_all('p',class_='question_content')
    for q in question:
        question1 = q.get_text()
        question1 = question1.replace('\n','')
        question2.append(question1)
print(len(question1))

table = pd.DataFrame({'name':name2,'score':score2,'content':content2})
print(table)

其中爬取时,如果遇到了查看全文选项,证明此时的评论在另一个页面中我们只需要进入另一个页面去进行搜索就行了,并把第一条评论选取下来。

对原网页进行解析:
利用获取到的conpanyID找到对应的公司,然后设置好页数总数,对每一个利用得到的参数合成url地址,利用request函数得到地址response请求,将response请求利用beautifulsoul进行解析,将解析后的参数分别提取下来。
分别是评论者的名字(选择了非匿名用户)、评价的星级(具体数字)、评论者的具体评论(注意,如果评论内容中有‘查看详情’,则进入父节点并解析前一个网址的评论)。
最后得到了用户的评论,利用星级将其打上标签,规定2星及以上为正向评论,一星及以下为负向评论,最后得到原始的数据集。

猜你喜欢

转载自blog.csdn.net/weixin_41611045/article/details/102766989
今日推荐