Write a CSDN hot list notification script in python, and don't miss your ranking changes ❤ Attached source code

❤️ This article is included in Learning python from actual combat Column, using python to realize practical cases in various directions such as crawler, office automation, data visualization and artificial intelligence, which is interesting and useful! ❤️

Click here to skip to the end Get source code and fan benefits

Life has expectations and unexpected surprises.

preface

Hello, everyone, I'm one

I've been writing in Station C for more than half a year. I can't help but see if I'm on the hot list every time I send an article. What's the number one

The official hot list news is only notified once when entering the top 50, and there will be a delay

As a result, I often spend time looking at my ranking. If the ranking is low, I have to turn down every time I open the web page for a long time

Later, I also learned that the original red heart of the fire at station C ❤️ In fact, it's just a sign that bloggers want to quickly locate their hot list position

To solve this problem, a script is written in python

The functions are as follows:

  • Climb the hot list once an hour
  • If you find yourself on the list, record the ranking, title and other information
  • Send the above information by email to notify yourself

In this way, you can know your hot list ranking anytime and anywhere, which also saves a lot of time.

Let's write this script together today

Effect display

Climb hot list

The hot list of CSDN has no login verification, so you can send a request with a header.

Parse the returned json string to get the title, ranking, popularity and other information

It is simple here. Please see the code for the basic operation of the crawler

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}

for i in range(2):
    res = requests.get("https://blog.csdn.net/phoenix/web/blog/hotRank?page={}&pageSize=50".format(i),headers=headers).json()
    if res["code"] == 200:
        data = res["data"]
        for d in data:
            userName=d["userName"]
            if(userName=="your csdn_id"):
                hotRankScore=d["hotRankScore"]
                nickName = d["nickName"]
                articleTitle = d["articleTitle"]
                articleDetailUrl = d["articleDetailUrl"]
                viewCount=d["viewCount"]
                commentCount=d["commentCount"]
                favorCount=d["favorCount"]

You need to change if(userName=="your csdn_id"): to your own ID.

Search ranking

Because there is no direct return to the ranking, it is divided into two pages to check

Here, calculate the ranking yourself

sorted=data.index(d)+1+i*50

Send mail

Before sending mail, you need to set up your mailbox. For specific tutorials, please refer to this article: Do not play microblogging, an email can know the real-time hot list, Tianxiu eats melons

This paper uses qq email teaching.

import smtplib
from email.mime.text import MIMEText

def send_mail(message,sorted):
    mail_host = 'smtp.qq.com'
    mail_user = '2865866423'
    mail_pass = 'vkpurabwifwwdhfd'

    # The sender can send it to itself
    sender = '2865866423@qq.com'
    # E-mail address of the mail recipient. You can write more
    receivers = ['2865866423@qq.com']

    # For email content setting, modify the first parameter to the content you want to send
    message = MIMEText(message, 'plain', 'utf-8')
    # Mail subject
    message['Subject'] = 'CSDN Hot list notification-{}'.format(sorted)
    # sender information 
    message['From'] = sender
    # Recipient information
    message['To'] = receivers[0]

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host)
        # Log in to the server
        smtpObj.login(mail_user, mail_pass)
        # send out
        smtpObj.sendmail(
            sender, receivers, message.as_string())
        # sign out
        smtpObj.quit()
        print('Mail sent successfully')
    except smtplib.SMTPException as e:
        print('Mail sending failed', e)  # Print error

This is a general code, reusable.

Mail content

This step is to build the email content, which can be customized or just use mine.

The content is also written to the log file while sending the message

time=time.strftime("%m-%d %H:%M", time.localtime())
                message="Current time:{}\n" \
                        "honorific{}: \n" \
                        "Congratulations on your creation<{}>Has entered the comprehensive hot list of the whole station!\n" \
                        "Ranking:{} \n" \
                        "degree of heat:{} \n" \
                        "Views:{} \n" \
                        "Number of comments:{} \n" \
                        "Number of collections:{} \n" \
                        "Article link:{} \n".format(time,nickName,articleTitle,sorted,hotRankScore,viewCount,commentCount,favorCount,articleDetailUrl)
                with open("log.txt","a+",encoding='utf-8') as f:
                    f.write(message+"\n")
                send_mail(message,sorted)

For those who are not on the list and have problems with the program, they can also be notified by email

    else:
        send_mail("Query failed. The program is faulty. Please repair it in time!")
if(message==""):
    print("Sorry, you are not on the list!")
    with open("log.txt","a+",encoding='utf-8') as f:
        f.write("Sorry, you are not on the list!")
    send_mail("Sorry, you are not on the list!","0")

Complete source code

Click the card at the end of the document, pay attention and reply to "CSDN" to get it

If the card is under review, please send it to the private blogger.

🌈 Treasure hunt

⭐ Today is the 63rd / 100th day to insist on writing more questions

⭐ Your likes, concerns, collections, comments and subscriptions are the biggest driving force for creation

In order to give back to all fans and return gifts, we have prepared a high-quality resource accumulated over the years, including learning videos, interview materials, collection of e-books, etc

Click the card below to receive it 👇👇👇

Tags: Python AI crawler csdn

Posted on Mon, 20 Sep 2021 05:01:53 -0400 by swordske