自制爬虫,爬取分类总阅读量,总评论量。全部文章阅读量和,以及评论量和。但是发现数据不对

版权声明:转载请声明原文链接地址,谢谢! https://blog.csdn.net/weixin_42859280/article/details/85418363
def tt(a,name):
    global ss
    global cc
    sum = 0
    com = 0
    pages = 10
    x = 1
    nn = []
    mm = []

其中global,很重要。没有这个global会报错误!!
全部代码:

import requests
import re
ss = 0
cc = 0
empty = []
def tt(a,name):
    global ss
    global cc
    sum = 0
    com = 0
    pages = 10
    x = 1
    nn = []
    mm = []
    base_url = "https://blog.csdn.net/weixin_42859280/article/category/"
    print('\n-------------------------'+name+'-------------------------')
    print('-------------------------下面是原创的-------------------------\n')
    for x in range(pages):
        w = 0
        r = requests.get(base_url+str(a)+'/'+str(x+1)+'?t=1&orderby=ViewCount')
        titles = re.findall(r'<span class="article-type type-.*?">\n.*?</span>\n(.*?)</a>', r.content.decode(), re.MULTILINE)
        visits = re.findall( r'<span class="read-num">阅读数:(.*?)</span>', r.content.decode())
        mm = re.findall( r'<span class="read-num">评论数:(.*?)</span>', r.content.decode())
        nn = [int(x) for x in visits] #将阅读数转换为数字
        nn = nn[1:]
        mm = mm[1:]
        n = 1
        for x, y, z in zip(titles, nn,mm):
            ff = open(name+'.txt','a')
            if n%10 == 0:
                ff.write(titles[w]+' 阅读数:'+str(nn[w])+' 评论数:'+mm[w]+' \n\n')
            else:
                ff.write(titles[w]+' 阅读数:'+str(nn[w])+' 评论数:'+mm[w]+' \n')
            ff.close()
            n += 1
            #if int(nn[w]) > 1000:
            #if int(mm[w]) > 0:
            print(titles[w]+' \t\t阅读数:'+str(nn[w])+' \t\t评论数:'+mm[w])
            sum += int(nn[w])
            com += int(mm[w])
            w+=1
    print('\n-------------------------下面是转载的------------------------')

    ff = open(name+'.txt','a')
    ff.write(' \n\n 下面就是转载的!\n\n')
    ff.close()

    pages = 11
    x = 1
    nn = []
    mm = []
    for x in range(pages):
        w = 0
        r = requests.get(base_url+str(a)+'/'+str(x+1)+'?t=2&orderby=ViewCount')
        titles = re.findall(r'<span class="article-type type-.*?">\n.*?</span>\n(.*?)</a>', r.content.decode(), re.MULTILINE)
        visits = re.findall( r'<span class="read-num">阅读数:(.*?)</span>', r.content.decode())
        mm = re.findall( r'<span class="read-num">评论数:(.*?)</span>', r.content.decode())
        nn = [int(x) for x in visits] #将阅读数转换为数字
        nn = nn[1:]
        mm = mm[1:]
        n = 1
        for x, y, z in zip(titles, nn,mm):
            ff = open(name+'.txt','a')
            if n%10 == 0:
                ff.write(titles[w]+' 阅读数:'+str(nn[w])+' 评论数:'+mm[w]+' \n\n')
            else:
                ff.write(titles[w]+' 阅读数:'+str(nn[w])+' 评论数:'+mm[w]+' \n')
            ff.close()
            n += 1
            #if int(nn[w]) > 1000:
            #if int(mm[w]) > 0:
            print(titles[w]+' \t阅读数:'+str(nn[w])+' \t评论数:'+mm[w])
            sum += int(nn[w])
            com += int(mm[w])
            w+=1
    ss += int(sum)
    cc += int(com)
    print("总阅读量:"+str(sum)+"  总评论量:"+str(com))
    ww = name+"  总阅读量:"+str(sum)+"   总评论量:"+str(com)
    empty.append(ww)
    ff = open(name+'.txt','a')
    ff.write(' \n 总阅读量:'+str(sum))
    ff.write('   总评论量:'+str(com))
    ff.close()

tt(8100910,'基础啦')
tt(8125178,'转载啦,方便找! ')
tt(8128370,'基石一般的东西!')
tt(8128378,' 自己找的一点CMD命令!嘻嘻~ ')
tt(8251895,' Linux CENTOS')
tt(8252351,'网络交换机与路由器 ')
tt(8252355,' Windows ')
tt(8252363,'计算机网络')
tt(8252366,' Linux ')
tt(8276398,' 看起来比较牛X的一些小玩意 ')
tt(8292701,'小米开启Fn ')
tt(8300944,' python '+'.txt')
tt(8309287,' 网络空间安全学习笔记')
tt(8460562,'Python语言及其应用学习! ')
tt(8493893,' ACM争取每日一oj! ')
tt(8518453,'C,C++语言编程基础知识!')
tt(8529012,'算法问题 ')

print("全部文章总阅读量:"+str(ss)+"  全部文章总评论量:"+str(cc))
for i in empty:
    print(i)

执行后文件截图:
在这里插入图片描述
代码截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
最后:
在这里插入图片描述
留着以后用·
不过,具体实现过程没有写。
想学的话,给我留言。
我教你呀~

猜你喜欢

转载自blog.csdn.net/weixin_42859280/article/details/85418363