2018余额已不到两天,2018初的flag完成了几个?今天我利用Python爬虫和数据分析技术,分析我的2018年文章分享情况。
爬取的文章为简书网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年的数据,所以选择多爬一点,然后筛选,这里我们通过字符串的处理,把time数据切分年的数据。
data['year'] = data['pub_time'].str.split('T').str[0].str.split('-').str[0]
然后用布尔选择,筛选出2018年的数据,重新保存和读取。
data[data['year'] == '2018'].to_csv('2.csv',index=False)通过排序,看看2018我的爆文是哪些?

6月份是高产的一个月,3-5月断更,9月断更。 3-5月,在苏大联培,9月不记得为什么不更了~

三个点:9、下午4、8点。 其实有时间我都会屯几篇文章,早上起来发(公众号首发),导致早上的最多,其实文章大部分都是晚上写的。。。

长沙下雪了 你的城市了?