已更新:http://blog.csdn.net/saywhat_sayhello/article/details/79450014
背景
我在今年年初有个计划是对python的爬虫进行一些学习,同时争取在年底CSDN个人访问量达到5万。然后我就想到写一个爬虫对CSDN个人的访问量进行记录。
实现
首先应该先了解怎么获取相关信息:Python爬虫学习–获取CSDN个人的访问量 :http://blog.csdn.net/saywhat_sayhello/article/details/79189455
设置定时任务:https://jingyan.baidu.com/article/ca00d56c767cfae99febcf73.html;http://blog.csdn.net/wwy11/article/details/51100432
添加时间信息:
http://www.runoob.com/python/python-date-time.html
然后将其中的代码换成:
import requests,time
from bs4 import BeautifulSoup
res = requests.get('http://blog.csdn.net/sayWhat_sayHello?ref=toolbar')
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,"html.parser")
blog_rank = soup.find(id='blog_rank')
time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f = open('Views.txt','a')
f.write(blog_rank.select('li')[0].getText()+" ")
f.write("获取时间:"+time+"\n")
f.close()