python爬虫之------每天给她(他)一个小故事啦啦啦啦

every blog every motto: Therefore ,send not to know for whom the bell tolls, It tolls for thee.
前言:
每天一个睡前小故事,给心爱的她(他)发一封啦啦啦
库: requests、smtplib、bs4等
环境: python3.7、ubuntu16.04
所需知识: python基础语法、requests。
状态: 本博文还在完善中……

一、代码部分

0.主程序

def main():
    for i in range(1, 11):  # 爬取页数范围
        if i == 1:
            url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html'
        else:
            url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index_{}.html'.format(i)

        print('正在爬取第{}页的故事'.format(i))

        # 提取一个网页html
        html = get_html(url)

        # 获取一页网页中所有故事的名称及链接
        get_all_story_link(html)
        time.sleep(0.15)

    # 所有链接爬去完成,打个包
    package_name_link = packaging(name_list, url_list)

    print('--->>>>>>>>爬取完成啦---------发邮件咯------------')
    random_choice_name_link = random.choice(package_name_link)  # 随机选择故事链接

    name_random_choice = random_choice_name_link[0]
    url_random_choice = random_choice_name_link[1]
    print('发送的故事名和链接:')
    print(name_random_choice)
    print(url_random_choice)

    # 发送故事
    send_story_to_email(name_random_choice, url_random_choice)

1.获取原网页html

def get_html(url):
    ''' 获取原网页链接'''
    try:
        res = requests.get(url, headers=headers, timeout=30)
        res.raise_for_status()  # 如果状态不是200,引发HTTPerror异常
        # t1 = res.encoding  # 从HTTP header中猜测响应内容编码方式
        # t2 = res.apparent_encoding # 从内容分析出响应编码方式
        res.encoding = res.apparent_encoding  # 根据网页内容分析出编码方式
        # res.encoding='utf-8'  # 转码
        # print(t1,t2)

        return res.text

    except:
        return '产生异常'

2. 获取所有故事的名字和其链接

def get_all_story_link(html):
    '''获取每页中故事的名称及链接,存在列表中'''
    url_based = 'http://www.tom61.com/'
    soup = BeautifulSoup(html, 'lxml')
    html_part = soup.find(class_='txt_box')  # 获取一页中故事的标签
    # print(html_part)
    a_tag = html_part.find_all(name='a')  # 获取所有故事链接标签,存在列表中
    # print('-'*100)
    # print(a_tag)

    # 循环遍历存储链接和故事名
    for temp in a_tag:
        url_list.append(url_based + temp.get('href'))  # 提取每个故事的链接,追加到列表中
        name_list.append(temp.get('title'))  # 提取每个故事的名称,追加到列表中

3. 打包成[(story_name,story_link),…]

def packaging(story_name, story_url):
    '''打包'''
    package_name_link = list(zip(story_name, story_url))
    print('查看打包')
    print(package_name_link)

    return package_name_link

4. 发送故事到指定邮箱

def send_story_to_email(story_name, url):
    '''发送故事到指定邮件'''

    msg_from = '[email protected]'  # 发送方邮箱
    passwd = '   '  # 填送发送方邮箱授权码,此处可百度获得发送方邮箱授权码
    receivers = ['[email protected]']  # 收件人邮箱

    subject = '今日份的睡前故事--->>>{}'.format(story_name)  # 主题

    # 获得故事原网页
    html_story = get_html(url)

    # 提取故事的文字内容
    content = get_story_content(html_story)

    # 以下为发送
    msg = MIMEText(content)  # 发送内容
    msg['Subject'] = subject  # 主题
    msg['From'] = msg_from  # 发送方
    msg['To'] = ','.join(receivers)

    try:

        s = smtplib.SMTP_SSL('smtp.qq.com', 465)  # 邮件服务器及端口号
        s.login(msg_from, passwd)
        s.sendmail(msg_from, msg['To'].split(','), msg.as_string())

        print('邮件发送成功')
    except:
        print('发送失败')
    finally:
        s.quit()

5.成果

在这里插入图片描述

二、文字部分

1. 通过本篇你将掌握

  1. 针对不同网址的请求
        if i == 1:
            url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index.html'
        else:
            url = 'http://www.tom61.com/ertongwenxue/shuiqiangushi/index_{}.html'.format(i)

  1. 抛出异常(判断状态码是否为200)
        res.raise_for_status()  # 如果状态不是200,引发HTTPerror异常
  1. 两种转码方式
  • 从HTTP header中猜测响应内容编码方式
        # t1 = res.encoding  # 从HTTP header中猜测响应内容编码方式
  • 从内容分析响应编码方式
        # t2 = res.apparent_encoding # 从内容分析出响应编码方式

  1. join的使用,将序列分开
    content = '\n'.join(text)  # 每段分开

2. 结束语

  1. contact by email: [email protected]
  2. 源码:https://github.com/onceone/Spiders

参考文章

[1] https://blog.csdn.net/weixin_44193909/article/details/88959522#commentBox
[2] https://www.jianshu.com/p/d78982126318
[3]https://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html
[4] https://docs.python.org/3.4/library/email-examples.html
[5] https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment

发布了39 篇原创文章 · 获赞 32 · 访问量 5805

猜你喜欢

转载自blog.csdn.net/weixin_39190382/article/details/89498881
今日推荐