爬取看准网公司点评

2017-04-09  本文已影响0人  蝈蝈大人

因为即将到某家公司面试,但网上对该公司的评价不好,所以我去查看了全部评论,突发奇想我明明会爬虫了,干嘛还呆逼地10段10段地加载,所以有了下面的代码,有缺陷存在。。。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

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="544397501@qq.com"; __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(i) + 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)

简单说下代码,由于看准网的评论是用JS加载的,所以要用到抓包,直接上截图教程。


图片.png

打开Chrome浏览器,然后F12,接着点击Network,勾选Preserve log,选择XHR,右键重新加载。拖到最下面的点击查看更多。


图片.png
这时Name列表中会出现很多网址,找到连续出现的,如图是listmore这个网址,点开可以看到里面的Request URL,其中会有"pageNum="的字段,这就是存放页面的字段,自己遍历一个数字范围就能实现爬取多个页面了,这个过程就是抓包了。 图片.png

最后,因为看准网貌似改版了,里面有1、问答式;2、用户自己的评论。该死的把text内容全放在同一个class里面,这里看不懂的话自己看下源代码就知道了,所以我的代码原本是打算将多个变量组成DataFrame,方便以后分析的。可惜,以上这个原因导致"question"这个变量的长度超过了其它的变量,放不进去,所以只能放弃了。
PS:我还不懂如何提取需要“查看全文”的部分,所以有的评论只爬到了部分,这是以后要学习的地方。

上一篇下一篇

猜你喜欢

热点阅读