Python transmission timing of the crawler micro channel message (17) to TA

Original difficult, please note bak main link address before reprint: Blessy_Zhu https://blog.csdn.net/weixin_42555080
environment this code:
operating platform: Windows
Python version: Python3.x
IDE: PyCharm

I. Introduction

Your circle of friends there will be some people, so you never got: also, or "you did not intend to drafts, but why solitary arrogant lead torrents I Chui Mei Weng ferry, but only that preference Lennon." That's TA; also or. "motherly hard on clothes line, the enemy treasure trunk in Baiyun book worthy of mangrove shift Boating, myriad Qian Feng Jie Lu proposed" mother;. also, or "sunset oblique, autumn cold tonight enemy comes or not, teach who stand to make Indus shadow "friend. As the saying goes, better than distant neighbors.
Friends for a long time did not contact
it because feelings have changed
but because life has changed
after all, the age of increased
responsibility shouldered heavy
care around more ......
why not like, write a program that gave TA sent daily, like TA, or TA is you want to say. Today, on the use of wxpy Python module, you want to say it issued to TA! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
 

 
Here Insert Picture Description

Second, the thinking process

First, select an object they want to send a message - I choose here is my brother (micro-letter name Note: brother), I want to give him the information is sent daily Zhengzhou weather, as well as to share a little story every day.
For weather Zhengzhou, through the website China weather [URL: http://www.weather.com.cn/ ] to access.
Here Insert Picture Description
Zhengzhou input, switching to Zhengzhou weather forecast, and the splice can be completed.
 

 
Here Insert Picture Description
This is a static page, not in the specific description of its crawling process.
Next, you want to crawl is a small story, through the website Children's Resource Network , [URL: http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html ] come crawling little story information.
Here Insert Picture Description
Just a point to open, such as "Happy Kingdom", you can see, this is a static page. Specific analysis is no longer go into details.
 
 
Here Insert Picture Description
Took over is to find a friend and send a message send, through the wxpy Python module to achieve:

from wxpy import *
bot = Bot()  # 登陆网页微信,并保存登陆状态
def sendmsg(content):
    # 搜索自己的好友,注意中文字符前需要+u
    my_group = bot.friends().search(u'***')[0]
    my_group.send(content)  # 发送MSG

Third, code implementation and the results show

Firstly, the code crawling small story:

# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import os
import time

def getHTMLText(url,headers):
    try:
        r=requests.get(url,headers=headers,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "爬取失败"

def parsehtml(namelist,urllist,html):
    url='http://www.tom61.com/'
    soup=BeautifulSoup(html,'html.parser')
    t=soup.find('dl',attrs={'class':'txt_box'})
    i=t.find_all('a')
    for link in i:
        urllist.append(url+link.get('href'))
        namelist.append(link.get('title'))
    print(urllist)
    print(namelist)
    return urllist,namelist

def main():
    headers = {'User-Agent':'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
               }
    urllist=[]#定义存放故事URL的列表
    namelist=[]#定义存放故事名字的列表
    if not os.path.exists('myshortStories'):#将所有的故事放到一个目录下
        os.mkdir('myshortStories')
    for i in range(1,20):#控制分页(每页有70个故事)
        if i==1:#爬取故事的地址和故事名
            url='http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html'
        else:
            url='http://www.tom61.com/ertongwenxue/shuiqiangushi/index_'+str(i)+'.html'
        print ("正在爬取第%s页的故事链接:" % (i))
        print (url+'\n')
        html=getHTMLText(url,headers)
        urls,storiesNames = parsehtml(namelist,urllist,html)
        littleStories = []
        m=0
        for url in urls:#通过URL在爬取具体的故事内容
            print('已经爬取了'+str(((i-1)*70+m))+'篇文章')
            littlestory = ''
            storyhtml = getHTMLText(url,headers)
            soup = BeautifulSoup(storyhtml, 'html.parser')
            t = soup.find('div', class_='t_news_txt')
            ptexts = t.find_all('p')
            for ptext in ptexts: #将一个故事作为数列的一个元素
                storytext = ptext.text
                littlestory = littlestory+storytext
            littleStories.append(littlestory.replace('\u3000\u3000',''))
            m=m+1
            time.sleep(1)
        myLittleStorySets = dict(zip(storiesNames,littleStories))#将故事名和故事内容拼接成字典的形式
        print("爬取链接完成")
        k=0
        for storyName, storyContent in myLittleStorySets.items():
            textName = 'myshortStories/'+'Day'+str(((i-1)*70+k))+'.txt'#为每个故事建立一个TXT文件
            with open(textName, 'a', encoding='utf-8') as f:
                f.write(storyName)
                f.write('\n')
                f.write(storyContent)
                k = k + 1
                print("正在写入Day"+str(((i-1)*70+k))+"故事")
                time.sleep(1)

if __name__=='__main__':
    main()

The results are as follows:
Here Insert Picture Description
Just open a crawling good article on display:
 

 
Here Insert Picture Description
Then get weather information:

def getZZWeatherAndSendMsg():
    # 要改为自己想要获取的城市的url,下面是郑州的url
    resp = urlopen('http://www.weather.com.cn/weather1d/10118010105A.shtml')
    soup = BeautifulSoup(resp, 'html.parser')
         # 获取温度数据
    tem = soup.find('p', class_="tem").find('span').string
        # 第一个包含class="tem"的p标签即为存放今天天气数据的标签
        # 获取天气状况
    weather = soup.find('p', class_="wea").string
        # 获取风力情况
    win = soup.find('p', class_="win").find('span').string
         # 获取日出时间
    sun = soup.find('p', class_="sun sunUp").find('span').string
     # 拼接要发送的消息格式
    contents = '郑州今日:' + '\n' + '天气:' + weather + '\n' + '温度:' + tem + '℃' + '\n' + '风力:' + win + '\n' + sun + '\n' + '注意天气变化!!'

Then assemble the information sent to the designated person:

    for i in range(100):
        fileName = './myshortStories/'+'Day'+str(i)+'.txt'
        storyContent = open(fileName,encoding='utf-8').read()
        sentContents = weatherContent+ '\n\n'+'每天给你讲一个小故事,今天的是:'+ '\n\n'+storyContent
        sendblogmsg(sentContents)
         # 设置每天发送一次v
        #t = Timer(24*60*60,job)
        #t.start()
        time.sleep(24 * 60 * 60)

The end result is:

Here Insert Picture Description

IV Summary

This article content is crawled two static websites, are "children's resource network" and "China Weather Network", and then climbed to the data, the interception sent to the designated person. This article on here, welcome bigwigs and more criticism and also are welcome to comment more exchanges.
 

 
Here Insert Picture Description

Attached complete code:

# -*- coding: utf-8 -*-
import requests
import os
import time
from urllib.request import urlopen
from bs4 import BeautifulSoup
from wxpy import *

bot = Bot()  # 登陆网页微信,并保存登陆状态
def sendblogmsg(content):
    # 搜索自己的好友,注意中文字符前需要+u
    my_group = bot.friends().search(u'**你要发送的对象微信备注名**')[0]
    my_group.send(content)  # 发送天气预报

def getHTMLText(url,headers):
    try:
        r=requests.get(url,headers=headers,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "爬取失败"

def parsehtml(namelist,urllist,html):
    url='http://www.tom61.com/'
    soup=BeautifulSoup(html,'html.parser')
    t=soup.find('dl',attrs={'class':'txt_box'})
    i=t.find_all('a')
    for link in i:
        urllist.append(url+link.get('href'))
        namelist.append(link.get('title'))
    print(urllist)
    print(namelist)
    return urllist,namelist

def mylittlestory():
    headers = {'User-Agent':'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
               }
    urllist=[]#定义存放故事URL的列表
    namelist=[]#定义存放故事名字的列表
    if not os.path.exists('myshortStories'):#将所有的故事放到一个目录下
        os.mkdir('myshortStories')
    for i in range(1,2):
        if i==1:#爬取故事的地址和故事名
            url='http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html'
        else:
            url='http://www.tom61.com/ertongwenxue/shuiqiangushi/index_'+str(i)+'.html'
        print ("正在爬取第%s页的故事链接:" % (i))
        print (url+'\n')
        html=getHTMLText(url,headers)
        urls,storiesNames = parsehtml(namelist,urllist,html)
        littleStories = []
        m=0
        for url in urls:#通过URL在爬取具体的故事内容
            print('已经爬取了'+str(((i-1)*70+m))+'篇文章')
            littlestory = ''
            storyhtml = getHTMLText(url,headers)
            soup = BeautifulSoup(storyhtml, 'html.parser')
            t = soup.find('div', class_='t_news_txt')
            ptexts = t.find_all('p')
            for ptext in ptexts: #将一个故事作为数列的一个元素
                storytext = ptext.text
                littlestory = littlestory+storytext
            littleStories.append(littlestory.replace('\u3000\u3000',''))
            m=m+1
            time.sleep(1)
        myLittleStorySets = dict(zip(storiesNames,littleStories))#将故事名和故事内容拼接成字典的形式
        print("爬取链接完成")
        k=0
        for storyName, storyContent in myLittleStorySets.items():
            textName = 'myshortStories/'+'Day'+str(((i-1)*70+k))+'.txt'#为每个故事建立一个TXT文件
            with open(textName, 'a', encoding='utf-8') as f:
                f.write(storyName)
                f.write('\n')
                f.write(storyContent)
                k = k + 1
                print("正在写入Day"+str(((i-1)*70+k))+"故事")
                time.sleep(1)

def getZZWeatherAndSendMsg():
    # 要改为自己想要获取的城市的url,下面是青岛的url
    resp = urlopen('http://www.weather.com.cn/weather1d/10118010105A.shtml')
    soup = BeautifulSoup(resp, 'html.parser')
     # 获取温度数据
    tem = soup.find('p', class_="tem").find('span').string
    # 第一个包含class="tem"的p标签即为存放今天天气数据的标签
    # 获取天气状况
    weather = soup.find('p', class_="wea").string
    # 获取风力情况
    win = soup.find('p', class_="win").find('span').string
     # 获取日出时间
    sun = soup.find('p', class_="sun sunUp").find('span').string
     # 拼接要发送的消息格式
    weatherContent = '郑州今日:' + '\n' + '天气:' + weather + '\n' + '温度:' + tem + '℃' + '\n' + '风力:' + win + '\n' + sun + '\n' + '注意天气变化!!'
    i=0
    for i in range(100):
        fileName = './myshortStories/'+'Day'+str(i)+'.txt'
        storyContent = open(fileName,encoding='utf-8').read()
        sentContents = weatherContent+ '\n\n'+'每天给你讲一个小故事,今天的是:'+ '\n\n'+storyContent
        sendblogmsg(sentContents)
         # 设置每天发送一次v
        #t = Timer(24*60*60,job)
        #t.start()
        time.sleep(24 * 60 * 60)

if __name__=='__main__':
    mylittlestory()
    getZZWeatherAndSendMsg()

Guess you like

Origin blog.csdn.net/weixin_42555080/article/details/90234015