日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

[python应用案例] 一.BeautifulSoup爬取天气信息并发送至QQ邮箱

發布時間:2024/6/1 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [python应用案例] 一.BeautifulSoup爬取天气信息并发送至QQ邮箱 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面作者寫了很多Python系列文章,包括:

  • Python基礎知識系列:Python基礎知識學習與提升
  • Python網絡爬蟲系列:Python爬蟲之Selenium+Phantomjs+CasperJS
  • Python數據分析系列:知識圖譜、web數據挖掘及NLP

??

接下來作者將學習并講解一些Python小應用,它將結合Python爬蟲、數據分析、web開發或其他功能進行介紹。一方面希望能提升讀者的Python學習興趣,另一方面也希望讀者能學到些知識,做些好玩的應用。本篇文章主要講解BeautifulSoup爬取每日天氣信息,然后將信息發送至QQ郵箱,其難點是如何配置QQ郵箱發送Python郵件。
基礎性應用文章,希望對您有所幫助,如果文章中出現錯誤或不足之處,還請海涵~


一. BeautifulSoup爬取天氣信息

1.分析網頁

中國天氣網:?http://www.weather.com.cn/weather/101260101.shtml
我們需要爬取貴陽市當天的天氣信息,比如“5月3日 陣雨 18/15℃”


接下來通過瀏覽器審查元素,可以看到這些信息位于<li class="sky skyid lv3 on">元素下,接著我定義class為“wea”和“tem”的元素進行定位,核心代碼為:
name = soup.find_all(attrs={"class":"sky skyid lv3 on"})



2.完整代碼

# -*- coding: UTF-8 -*- import urllib import urllib.request from bs4 import BeautifulSoup#下載數據 url = "http://www.weather.com.cn/weather/101260101.shtml" content = urllib.request.urlopen(url).read() soup = BeautifulSoup(content,"html.parser") #print(soup.title.get_text())content = "" name = soup.find_all(attrs={"class":"sky skyid lv3 on"}) for u in name:wea = u.find(attrs={"class":"wea"}).get_text()tem = u.find(attrs={"class":"tem"}).get_text()content = "天氣:" + wea + " 溫度:" + temcontent = content.replace("\n","")print(content)

輸出結果如下圖所示:




二. QQ郵箱設置STMP

在使用Python自動發送郵件之前,需要對我們的QQ郵箱進行簡單的配置,過程如下:

1.首先登陸QQ郵箱,選擇“賬戶”如下圖所示:



2.在賬戶頁面往下拉,看到“POP3/SMTP”設置,點擊開啟按鈕,如下圖所示:



3.彈出如下圖所示界面,然后發送這條短信至指定號碼,點擊“我已發送”按鈕。



4.彈出的提示中會顯示16位授權碼,注意一定要記住這個授權碼,后面寫Python代碼也需要,然后點擊“確定”按鈕。



5.接下來將收取選項設置為“全部”,并點擊“保存”按鈕即可。注意端口號如下:




三. Python自動發送郵件

Python 的 email 模塊里包含了許多實用的郵件格式設置函數,可以用來創建郵件“包裹”。使用的 MIMEText 對象,為底層的 MIME(Multipurpose Internet MailExtensions,多用途互聯網郵件擴展類型)協議傳輸創建了一封空郵件,最后通過高層的SMTP 協議發送出去。 MIMEText 對象 msg 包括收發郵箱地址、郵件正文和主題,Python 通過它就可以創建一封格式正確的郵件。smtplib 模塊用來設置服務器連接的相關信息。

Python SMTP 對象使用 sendmail 方法發送郵件,語法如下:(參考: Python SMTP發送郵件

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])

參數說明:

  • from_addr: 郵件發送者地址。
  • to_addrs: 字符串列表,郵件發送地址。
  • msg: 發送消息

這里要注意一下第三個參數,msg 是字符串,表示郵件。我們知道郵件一般由標題,發信人,收件人,郵件內容,附件等構成,發送郵件的時候,要注意 msg 的格式。這個格式就是 smtp 協議中定義的格式。


代碼如下:
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header#發送郵件 msg_from = "1455136241@qq.com" #授權碼(而不是密碼) EMAIL_HOST_PASSWORD = '****htacisgv****' #接受郵件 msg_to = "15201615157@163.com" #主題 subject = "Python測試代碼" #正文 content = "女神,這是我使用python smtplib及email模塊發送的郵件。" print(content)#MIMEText構建對象 參數分別是:郵件正文、MIMEsubtype中'plain'表示純文本、utf-8編碼 msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to#郵件服務器及端口號 #smtplib.SMTPServerDisconnected: Connection unexpectedly closed try:s = smtplib.SMTP_SSL("smtp.qq.com",465) #端口號s.set_debuglevel(1)s.login(msg_from, EMAIL_HOST_PASSWORD)s.sendmail(msg_from, msg_to, msg.as_string())print("發送成功") except s.SMTPException.e:print("發送失敗")print(e) finally:s.quit()

發送成功之后如下圖所示,注意login()輸入郵箱名和授權碼,而不是密碼。


運行過程輸出內容如下:

>>> 女神,這是我使用python smtplib及email模塊發送的郵件。 send: 'ehlo [192.168.0.101]\r\n' reply: b'250-smtp.qq.com\r\n' reply: b'250-PIPELINING\r\n' reply: retcode (250); Msg: b'smtp.qq.com\nPIPELINING\' send: 'AUTH PLAIN ADE0NTUxMzYyNDFAcXEuY29tAGVveXFodGFjaXNndmlmYmg=\r\n' reply: b'235 Authentication successful\r\n' reply: retcode (235); Msg: b'Authentication successful' send: 'mail FROM:<1455136241@qq.com> size=296\r\n' reply: b'250 Ok\r\n' reply: retcode (250); Msg: b'Ok' send: 'rcpt TO:<15201615157@163.com>\r\n' reply: b'250 Ok\r\n' reply: retcode (250); Msg: b'Ok' send: 'data\r\n' reply: b'354 End data with <CR><LF>.<CR><LF>\r\n' reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>' data: (354, b'End data with <CR><LF>.<CR><LF>') send: b'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\n...r\nTo: 15201615157@163.com\r\n' reply: b'250 Ok: queued as \r\n' reply: retcode (250); Msg: b'Ok: queued as' data: (250, b'Ok: queued as') 發送成功 send: 'quit\r\n' reply: b'221 Bye\r\n' reply: retcode (221); Msg: b'Bye' >>>


四. 完整代碼實現

完整代碼如下所示:

# -*- coding: UTF-8 -*- import urllib import urllib.request from bs4 import BeautifulSoup#下載數據 url = "http://www.weather.com.cn/weather/101260101.shtml" content = urllib.request.urlopen(url).read() soup = BeautifulSoup(content,"html.parser") content = "" name = soup.find_all(attrs={"class":"sky skyid lv3 on"}) for u in name:wea = u.find(attrs={"class":"wea"}).get_text()tem = u.find(attrs={"class":"tem"}).get_text()content = "天氣:" + wea + " 溫度:" + temcontent = content.replace("\n","")print(content)#發送郵件 import smtplib from email.mime.text import MIMEText from email.header import Headermsg_from = "1455136241@qq.com" EMAIL_HOST_PASSWORD = '****htacisgv****' msg_to = "15201615157@163.com" subject = "Python爬取天氣" other = content + "\n這是我使用python smtplib及email模塊發送的郵件。" print(other)msg = MIMEText(other,'plain','utf-8') msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_totry:s = smtplib.SMTP_SSL("smtp.qq.com",465)s.set_debuglevel(1)s.login(msg_from, EMAIL_HOST_PASSWORD)s.sendmail(msg_from, msg_to, msg.as_string())print("發送成功") except s.SMTPException.e:print("發送失敗")print(e) finally:s.quit()

發送成功之后如下圖所示:

需要注意,代碼有時能發送成功,有時報錯“smtplib.SMTPServerDisconnected: Connection unexpectedly closed”,網上說是設置端口465的原因,但作者已經設置了的,不知道為什么?希望博友幫忙。

?(By:Eastmount 2018-5-3 下午4點 ??http://blog.csdn.net/eastmount/


總結

以上是生活随笔為你收集整理的[python应用案例] 一.BeautifulSoup爬取天气信息并发送至QQ邮箱的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。