用Python看我的2018

版权声明:禁止转载至其它平台,转载至博客需带上此文链接。 https://blog.csdn.net/qq_41841569/article/details/85387392

用Python看我的2018

前言

2018余额已不到两天,2018初的flag完成了几个?今天我利用Python爬虫和数据分析技术,分析我的2018年文章分享情况。

Python爬虫

用Python看我的2018

爬虫分析

爬取的文章为简书网2018年的文章,为什么不选择公众号了?

  • 简书好爬
  • 公众号有转载
  • 首先,简书分页使用了异步加载,我们先找下包,发现url很规律,换下page后面的数字就可以了。

用Python看我的2018

现在还需要考虑的是:怎么确保文章都是2018年的。这里的处理方法为:首先多爬一些,然后在数据分析中进行筛选,选出2018年的数据即可。

爬虫代码

import requests
from lxml import etree
import csv
import time
headers={
 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
f = open('1.csv','w+',encoding='utf-8',newline='')
writer = csv.writer(f)
writer.writerow(['title','view','comment','good','pub_time'])
def get_info(url):
 res = requests.get(url,headers=headers)
 html = etree.HTML(res.text)
 infos = html.xpath('//ul[@class="note-list"]/li')
 for info in infos:
 title = info.xpath('div/a/text()')[0]
 view = info.xpath('div/div/a[1]/text()')[1].strip()
 comment = info.xpath('div/div/a[2]/text()')[1].strip()
 good = info.xpath('div/div/span[1]/text()')[0].strip()
 pub_time = info.xpath('div/div//span[@class="time"]/@data-shared-at')[0]
 print(title,view,comment,good,pub_time)
 writer.writerow([title,view,comment,good,pub_time])
if __name__ == '__main__':
 urls = ['https://www.jianshu.com/u/9104ebf5e177?order_by=shared_at&page={}'.format(str(i)) for i in range(1,6)]
 for url in urls:
 get_info(url)
 time.sleep(2)

2018文章分析

前文提到,我们没办法确定保证全是2018年的数据,所以选择多爬一点,然后筛选,这里我们通过字符串的处理,把time数据切分年的数据。

data['year'] = data['pub_time'].str.split('T').str[0].str.split('-').str[0]

用Python看我的2018

然后用布尔选择,筛选出2018年的数据,重新保存和读取。

data[data['year'] == '2018'].to_csv('2.csv',index=False)

总体情况

  • 2018总共写了42篇文章,相比于2017年,是少了很多的。
  • 平均阅读量388,这个比公众号高很多
  • 平均点赞7.5,低低低~
  • 总体来说,并不理想,还是要多输出呀~

阅读量最高的五篇文章

通过排序,看看2018我的爆文是哪些?

用Python看我的2018

  • 杨超越一文阅读量最多,果然是锦鲤
  • 爬虫+数据分析文(结合热点)更容易上榜
  • 广告忽略
  • 机器学习系列也还不错

2018年文章月份分布

6月份是高产的一个月,3-5月断更,9月断更。

3-5月,在苏大联培,9月不记得为什么不更了~

用Python看我的2018

哪个时间段发文

三个点:9、下午4、8点。

其实有时间我都会屯几篇文章,早上起来发(公众号首发),导致早上的最多,其实文章大部分都是晚上写的。。。

用Python看我的2018

结语

长沙下雪了

你的城市了?

猜你喜欢

转载自blog.csdn.net/qq_41841569/article/details/85387392