python爬取网易动态评论

打开网易的一条新闻的源代码后,发现并没有所要得评论内容。
经过学习后发现,源代码只是一个完整页面的“骨架”,而我所需要的内容是它的填充物,这时候需要打开工具里面的开发人员工具,从加载的“骨肉”里找到我所要的评论这里写图片描述圈住的是类型
找到之后打开网页,发现json类型的格式,用我已学过的正则,bs都不好闹,于是便去了解了正则,发现把json的格式换化成python的格式后,用列表提取内容是一条明朗的道路。。。
但是在细致分析的时候也发现了问题
这里写图片描述从这里获得每条评论时,感觉有点不对,观察发现如果是回复评论的评论会出现他回复那条评论的数据,于是用正则提取了一下
最终的代码如下:

#coding=utf-8
__author__ = 'kongmengfan123'

import urllib
import re
import json
import time


def gethothtml(url):#最热评论
    page=urllib.urlopen(url)
    html=page.read()
    get_json(html)




def gethnewtml():#最新评论有5页

    for i in range(1,6):
        url = 'http://comment.news.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/C4QFIJNS0001875O/comments/newList?offset=%d&limit=30&showLevelThreshold=72&headLimit=1&tailLimit=2&callback=getData&ibc=newspc&_=1478010624978'%i*30
        page = urllib.urlopen(url)
        html=page.read()
        time.sleep(1)
        get_json(html)






def get_json(json_):

    end_=re.compile(r'\);')#将json网页转化成python数据
    begain=re.compile(r'getData\(')
    json_=begain.sub('',json_)
    json_=end_.sub('',json_)
    ajson=json.loads(json_)
    lis=ajson["commentIds"]#获得每条评论的键
    n=0
    for i in range(1,len(lis)):

        try:
            xulie=re.compile('\d{10,}')#取得准确评论的键(去掉回复)
            bia=re.findall(xulie,lis[n])
            w.write(ajson['comments'][bia[len(bia)-1]]['user']['nickname'].encode('utf-8')+'|')
        except KeyError:
            w.write(ajson['comments'][bia[len(bia)-1]]['user']['location'].encode('utf-8')+'|')


        if (len(lis[n])>13):
            xulie=re.compile('\d{10,}')
            bia=re.findall(xulie,lis[n])

            w.write(ajson['comments'][bia[len(bia)-1]]['content'].encode('utf-8')+'\n')
        else:
             w.write(ajson['comments'][lis[n]]['content'].encode('utf-8')+'\n')
        n=n+1
    return lis





w=open('wangyi.txt','w')
w.write('用户名'+'|'+'热门评论'+'\n')
hot_=gethothtml('http://comment.news.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/C4QFIJNS0001875O/comments/hotList?offset=0&limit=40&showLevelThreshold=72&headLimit=1&tailLimit=2&callback=getData&ibc=newspc')
w.write('用户名'+'|'+'最新评论'+'\n')
gethnewtml()
w.close()

成功。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值