爬取天氣資訊並郵件傳送

2021-09-29 06:01:53 字數 3041 閱讀 2599

直接上**

#!/usr/bin/env python

#coding=utf-8

from urllib.request import urlopen

from pyquery import pyquery as pq

import smtplib

from email.mime.text import mimetext

import codecs

import time

defsentemail

(receiver)

: citycode=

'101020100'

#南京**

url=

''+citycode+

'.shtml'

html = urlopen(url)

.read(

).decode(

'utf-8'

) doc=pq(html)

today_date=doc(

'.sky.skyid.lv3.on h1'

).text(

) wea=doc(

'.sky.skyid.lv3.on p'

).text(

) tem=doc(

'.sky.skyid.lv3.on.tem i'

).text(

) wind=doc(

'.sky.skyid.lv3.on .win em span'

).attr(

'title'

) wind_power=doc(

'.sky.skyid.lv3.on .win em'

).text(

) light_power=doc(

'.hide.show .clearfix .li1 span'

).text(

) light_power_way=doc(

'.hide.show .clearfix .li1 p'

).text(

) feel_index=doc(

'.hide.show .clearfix .li3.hot span'

).text(

) wear_index=doc(

'.hide.show .clearfix .li3.hot p'

).text(

) popu_index=doc(

'.hide.show .clearfix .li6 span'

).text(

) popu_index_2=doc(

'.hide.show .clearfix .li6 p'

).text(

) tem7d_low=doc(

'.tem i'

).text()[

4:] tem7d_high=doc(

'.tem span'

).text(

) today= time.strftime(

"%y-%m-%d"

,time.localtime(time.time())

) qianyan=''+

'地點:上海'+'

'+''+

'今天是:'

+today+

'日,下面奉上今日天氣'+'

' jieshuyu=''+

'rush!!!'+'

' body=

''+wea+''

+''+wind+''

+''+wind_power+''

+''+'紫外線指數:'

+light_power+''

+''+'紫外線應對:'

+light_power_way+''

+''+'體感:'

+feel_index+''

+''+'穿衣指數:'

+wear_index+''

+''+'空氣汙染擴散指數:'

+popu_index_2+''

+''+'未來7天溫度最高溫度:'

+tem7d_high+''

+''+'未來7天溫度最低溫度:'

+tem7d_low+''

host =

'smtp.163.com'

#發件伺服器

all=qianyan+body+jieshuyu

port =

465#發件埠號

sender =

'example@163.com'

#傳送端的賬號

pwd =

'password'

#郵件密碼,第三方為授權碼

msg = mimetext(

all,

'html'

,'utf-8'

)#賦值msg

msg[

'subject']=

'天氣預報'

#郵件標題

msg[

'from'

]= sender #顯示的傳送人

msg[

'to'

]= receiver #顯示的收信人

try:

s = smtplib.smtp_ssl(host, port)

s.login(sender, pwd)

print

('yes'

) s.sendmail(sender, receiver, msg.as_string())

print

('done.sent email success'

)for i in

range

(43200):

time.sleep(1)

print

(43200

-i) sentemail(

'example@qq.com'

)except smtplib.smtpexception:

print

('error.sent email fail'

)sentemail(

'example@qq.com'

)

Python 爬取天氣資訊

第一次python部落格,僅作紀念。import requests import re from bs4 import beautifulsoup requests庫從網上獲取資源,re bs4 庫,用來提取需要的資訊。開啟要爬取的 右擊檢視其源 找到感興趣的內容,如下 2020年01月12日 星期...

爬取某地全年天氣資訊

coding utf 8 created on sat apr 11 13 36 42 2020 author zan import requests import json import pandas as pd import re from bs4 import beautifulsoup he...

python 爬取天氣並傳送郵箱

以中國天氣網為例 www.weather.com.cn 主要的實現步驟有 一 對中國天氣網進行爬取 二 通過郵箱傳送提醒 完成 import requests from bs4 import beautifulsoup import smtplib from email.mime.text impo...