In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Python to send a love message to the goddess at a fixed time every day. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Without saying much, let's take a look at it.
My diary is fine on April 23rd.
You haven't answered my message for three days, and under my tireless harassment, you are finally willing to reply to me. You said, 'nmsl', I think it must mean something! Oh! It dawned on me that it was nimesulide granules.
She knew I had arthritis and let me eat nimesulide granules. She still cared about me but didn't want to be so enthusiastic.
Oh! She is so aloof. I seem to like her more.
You see, although the goddess often ignores me, she still secretly cares about me. Although she doesn't say it directly, I know it all.
The only problem is that I have been so busy lately that I have no time to send "good morning" to the goddess. No! "good morning" can not be broken, this is my sincere love for the goddess, although the goddess will not reply to me, but she must keep it all in mind, if she does not reply to me, she must be testing me!
As an omnipotent programmer, I immediately thought why not use Python to crawl love words and send them to her at a fixed time every day.
Crawl for love words
Select love words resources
First of all, we need to find the right resources of love words on the Internet.
Analyze web page resources
1. Locate the resources of love words
Mouse over the love words we want to crawl and quickly press the right button + check (element).
First, you can see where our love words are on the page.
two。 Positioning label
In the developer tool, we easily locate the tag of our love words, which is the content in the red box above.
3. Analyze the request mode
Refresh under the previous interface (do not quit developer mode):
Find the resource we requested under the Network column, and find the way to request the resource in his Headers. This is GET.
Use the urllib library to get resources
1. Install urllib library
This step is afraid that there is no urllib2 library in your server. It will cause trouble to add it later. If you do not add the library, please add the method by Baidu.
2. Request resource website
Code:
Import urllib.request
Url = "http://www.1juzi.com/new/150542.html"
Html = urllib.request.urlopen (url). Read ()
Print (html)
Results:
.
Use the command to display the following information.
News1 ()
1. Make sure that the configuration of the network address is correct, and the configuration of the interface is correct.
2. ®will make sure that the ©does not change the ®connection, the ®will be switched off, and the ®will not be switched on.
3. If there is a gap between the two interfaces, there is no difference between the number of users and the number of users.
4. Please check the command for the link table, and then check the command for the link.
5. Please send a message to the contact person, and if so, please send a message to the router. If so, please do not send a message.
6. If you want to send a copy of the cable that you have received, you will have to check the error in the queue that is available to you, so that you will not be able to do so, so that you will not be able to turn off the check on the router, so that you will have to check the value of the check on the router.
7. Do you want to check the number of messages in your address?
8. Use the information provided by the QQ, use the QQ, and use the QQ, the QQ, the Q
9. If you want to make sure that you do not want to do so, you will have to make sure that you can make sure that you can make sure that you do not want to do so.
.
Question mark, do you have many friends?
What we climb down is garbled, sending a bunch of garbled codes to the goddess. The goddess may still think that it is a code we sent to her, and it may take time to find a way to decrypt it. I'm tired to think about it. I can't do this!
In fact, the reason for garbled code is that the coding mode of the main web page is different from that of our crawler, as long as we find the coding mode of the web page.
3. Set the decoding type
There are three ways:
1. Get the encoding method from the Content-Type of the web page
two。 Third-party library intelligent identification code, commonly used chardet, etc.
3. Guessing coding
We actually have the coding type in the Content-Type of the request header, but to be on the safe side, it is also for the purpose of applying to more resource websites. Here we demonstrate the usage of the following chardet. Please use Baidu to guess the use of the code.
Chardet gets the encoding type
Code:
Import urllib.request
Import chardet
Url = "http://www.1juzi.com/new/150542.html"
Html = urllib.request.urlopen (url). Read ()
Print ("charset in html header:", chardet.detect (html))
Results:
/ usr/bin/python3.7 / home/baldwin/PycharmProjects/IAmADog/spider/Spider.py
Charset in the html header: {'encoding':' GB2312', 'confidence': .99,' language': 'Chinese'}
Process finished with exit code 0
Get!!! The coding mode is GB2312!
Set decoding
Code:
Import urllib.request
Import chardet
Url = "http://www.1juzi.com/new/150542.html"
Html = urllib.request.urlopen (url). Read ()
Charset = chardet.detect (html) .get ("encoding")
HtmlText = html.decode (charset,errors = 'ignore')
Print (htmlText)
Results:
. A little love word every day news1 ()
1. Put you in the most important position in my heart and carve your name with my heart.
2. Love is stepping out of the world and looking forward to it; love is the reincarnation of a lifetime; I don't ask why I love you.
3. Drag you into your arms, give you a lifetime of companionship, hold you in your arms, and protect you for a lifetime of peace.
4. It's more than ambition. I've been premeditating all my life. Since I met you, I won't give up until I see the loess.
5. I thought about it later, I will not delay you, others will delay you, then I am not reconciled, I will delay you.
6. I didn't know what to do when I saw you. All I had was my heartbeat. You changed me. I've never done this before. If there's one person I can change willingly, it's you.
Accompany me to Hoh Xil to see the sea. Not the future, as long as you come.
8. My heart beats for you every day, I am moved by you every moment, and I worry about you every second. It feels good to have you.
.
Oh, ho! Got it!
4. Encapsulation code
Just now we have implemented the acquisition of resources, but this kind of code is too inconvenient to use, we encapsulate it in the method:
.
Import urllib.request
Import chardet
Def getHtml (url):
"
Get web page html text resources
: param url: links to web pages
: return: Web page text resources
"
Html = urllib.request.urlopen (url). Read ()
Charset = chardet.detect (html) .get ("encoding")
HtmlText = html.decode (charset, errors='ignore')
Return htmlText
Analysis of web page resources
If you parse a web page, you need a third-party plug-in Beautiful Soup to extract data from xml and HTML.
Get content node content
All the resources we want are under a div node with a class of "content". Let's get all the contents of this node first.
Part of the code:
Soup = BeautifulSoup (htmlText, "html.parser")
"get the contents of the content node"
Div_node = soup.find ('div', class_='content')
Print (div_node.get_text)
Results:
/ usr/bin/python3.7 / home/baldwin/PycharmProjects/IAmADog/spider/Spider.py
= len (sentenceList):
# text me when you run out
Send.sendSMSMsg (SEND_TO_ME, MY_TEL)
# Pop out
Break
# text the goddess
Content = sentenceList [index] + MSG_SUFFIX
Send.sendSMSMsg (content, SEND_TO_TEL)
# subscript plus one
Index + = 1
# because the time is set in seconds, it is paused for 2 seconds so that it will not be executed multiple times in 1 second
Time.sleep (2)
Tests in debug mode:
Short message sending test
In idea debug mode, you can set the time_now parameter to the time when we want to send an SMS message, and the SMS message is sent successfully:
Administrator notifies test
Similarly, in debug mode, set the value of index to len (sentenceList), and the program enters the notification administrator module, which is not demonstrated here.
Encapsulation
From spider import Spider
From send import Send
Import time
TIME_TO_DO = '08VOL30UR 00' # send point in time
MSG_SUFFIX ='\ nFrom your cutie-- Baldwin' # SMS suffix
SOURCE_URL = 'http://www.1juzi.com/new/150542.html' # love message resource address
SEND_TO_TEL ='+ 8618436354553'# the goddess's mobile phone number
SEND_TO_ME = 'licking the dog, the text message to the goddess has been used up, come and update!'
MY_TEL ='+ 8618436354553'# licking dog mobile phone number
Def doSend ():
Index = 0 # the subscript of the next text message
SentenceList = Spider.getSentenceList (SOURCE_URL) # list of love words
While True:
# Refresh
Time_now = time.strftime ("% H:%M:%S", time.localtime ())
# set the time of day here
If time_now = = TIME_TO_DO:
# actions to be performed
# determine whether the current list is running out
If index > = len (sentenceList):
# text me when you run out
Send.sendSMSMsg (SEND_TO_ME, MY_TEL)
# Pop out
Break
# text the goddess
Content = sentenceList [index] + MSG_SUFFIX
Send.sendSMSMsg (content, SEND_TO_TEL)
# subscript plus one
Index + = 1
# because the time is set in seconds, it is paused for 2 seconds so that it will not be executed multiple times in 1 second
Time.sleep (2)
# main program entry
If _ _ name__ = ='_ _ main__':
DoSend ()
Small summary
The main module used in this section: time, and then remember to import the first two modules we made ourselves.
Summary
This program is relatively easy, as long as follow the article step by step basically do not have problems with each other.
Finally, just run the entrance of the main program in our last module, and you can modify the configuration according to your own needs to achieve different results.
I'll tell the goddess now.
Hey! The goddess came back in a second, happy!
The above is how to use Python to send a love word to the goddess at a regular time every day. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.