-----------------------------------------------学无止境-----------------------------------------------html
前言:你们好,欢迎来到誉雪飞舞的博客园,个人每篇文章都是本身用心编写,python
算不上精心可是足够用心分享个人自学知识,但愿你们可以指正我,互相学习成长。web
转载请注明:http://www.javashuo.com/article/p-diomrqjf-cr.html网络
先给你们道个歉,个人上一篇文章中没有在树莓派的交互界面进行操做实验,并且我答应你们必定会把这个实验补上,虽然没太有空整理,app
可是本身说出来的承诺,我必需要兑现,认真对待个人每一篇文章,认真对待个人每一个读者,认真对待本身的心里,不能应付着就过去了,dom
因此我特地给挤出时间怀着愧疚与坚决地心情再次分享完善这个小项目。socket
一开始出了不少问题,果真仍是那句金句名言,切勿眼高手低函数
既然答应给你们一个交代,那我们一个个慢慢说:学习
一、上来直接运行有错误的说缺少bs四、beautifulsoup库,命令安装;测试
#python2:
sudo apt-get install python-bs4 sudo pip install beautifulsoup4
#python3:
sudo apt-get install python3-bs4
sudo pip3 install beautifulsoup4
关于报错信息:pip3无效命令的解决
首先安装setuptools(必定要用sudo管理员权限作,若是长时间链接不上主机Ctrl+C,从新执行命令)
cd /usr/local/src/ sudo wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz tar -zxvf setuptools-19.6.tar.gz cd setuptools-19.6/ python3 setup.py build python3 setup.py install
其次安装pip3(网络不佳很正常,多试几回多试几回)
cd /usr/local/src/ sudo wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz tar -zxvf pip-8.0.2.tar.gz cd pip-8.0.2/ python3 setup.py build python3 setup.py install
https://www.jianshu.com/p/d4c1a08b993f
测试安装成功,运行命令pip3成功
二、报错信息:
FeatureNotFound: Couldn't find a tree builder with the features you requeste
https://blog.csdn.net/qq_16546829/article/details/79405605
在报错代码中把函数参数中全部的"lxml"改为"html.parser"
#例子:
bs = BeautifulSoup(r, 'lxml').find(.... #改为 bs = BeautifulSoup(r, 'html.parser').find(....
html.parser是调用python解析器,可是没有解决lxml库不能用的问题啊
三、看到差异了吧,使用win10_url没法打开网页,你能够试试,因此问题就很严重了,咱们根本打不开网页,
因此后续的爬虫工做根本从源头就已经宣告over了.....这也是一个小细节。
四、中文编码报错信息与解决方案:
这个报错信息很显然就是一向的中文编码问题,让人很头疼,我加上了:
#!/home/pi/SmartHome/SMTP/ok' #*_*coding:utf-8_*_
但依然没卵用,随即我百度找到一位网友写的文章以下:
https://blog.csdn.net/weixin_39221360/article/details/79525341
注意若是你的设备同时安装了 Python 2.x 和 Python 3.x,你须要用 python3 运行
Python 3.x:
$python3 myScript.py
当你安装包的时候,若是有可能安装到了 Python 2.x 而不是 Python 3.x 里,就须要使用:
$sudo python3 setup.py install
若是用 pip 安装,你还能够用pip3 安装 Python 3.x 版本的包:
$pip3 install beautifulsoup4
https://blog.csdn.net/DoctorLDQ/article/details/73230026
解决方法有三中:
1.在命令行修改,仅本会话有效:
1)经过>>>sys.getdefaultencoding()查看当前编码(若报错,先执行>>>import sys >>>reload(sys));
2)经过>>>sys.setdefaultencoding('utf8')设置编码
2.较繁琐,最有效
1)在程序文件中如下三句
import sys
reload(sys)
sys.setdefaultencoding('utf8')
3.修改Python本环境(推荐)
在Python的Lib\site-packages文件夹下新建一个sitecustomize.py文件,内容为:
#coding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
重启Python解释器,发现编码已被设置为utf8,与方案二同效;这是由于系统在Python启动的时候,自行调用该文件,
设置系统的默认编码,而不须要每次都手动加上解决代码,属于一劳永逸的解决方法。
#在这里我是用的是如下方法,感受很方便,可是最后一个我感受很好一劳永逸不错不错,可是我没找着....
import sys reload(sys) sys.setdefaultencoding('utf8')
五、header的改变,这也是一处必定要注意的细节。
如何获取网页的header{ ...}方法我仍是简单说一下吧,会的自行略过,去本身电脑打开的网页copy吧。
打开url,F12 而后 Ctrl+R ,
按照如下格式进行选取,写入咱们的程序中。(不要漏掉逗号!!!!)
header = { 'authority':'h5tq.moji.com', 'accept':'image/webp,image/apng,image/*,*/*;q=0.8', 'accept-encoding':'gzip, deflate, br', 'accept-language':'zh-CN,zh;q=0.9', 'cache-control':'no-cache', 'pragma':'no-cache', 'referer':'https://tianqi.moji.com/weather/china/shandong/penglai', 'user-agent':'Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Raspbian Chromium/72.0.3626.121 Chrome/72.0.3626.121 Safari/537.36' }
六、....还没么,今下午几个小时整理的比较急 ,若是还有什么问题请留言评论或者私信我均可以。
1 #利用树莓派发送天气预报邮件
2 import time 3 import smtplib 4 import requests 5 import random 6 import socket 7 import bs4
8 import sys 9 reload(sys) 10 sys.setdefaultencoding('utf8') 11 from bs4 import BeautifulSoup 12 from email.mime.text import MIMEText 13 from email.header import Header 14
15 def get_content(url): 16 header = { 17 'authority':'h5tq.moji.com', 18 'accept':'image/webp,image/apng,image/*,*/*;q=0.8', 19 'accept-encoding':'gzip, deflate, br', 20 'accept-language':'zh-CN,zh;q=0.9', 21 'cache-control':'no-cache', 22 'pragma':'no-cache', 23 'referer':'https://tianqi.moji.com/weather/china/shandong/penglai', 24 'user-agent':'Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Raspbian Chromium/72.0.3626.121 Chrome/72.0.3626.121 Safari/537.36'
25 } 26 timeout = random.choice(range(80, 180)) 27 while True: 28 try: 29 rep = requests.get(url,headers = header,timeout = timeout) 30 rep.encoding = 'utf-8'
31 break
32 except socket.timeout as e: 33 print( '3:', e) 34 time.sleep(random.choice(range(8,15))) 35 except socket.error as e: 36 print( '4:', e) 37 time.sleep(random.choice(range(20, 60))) 38 return rep.text 39
40 def get_weather(url,data): 41 air_list = [] 42 weather_list = [] 43 #weather_status = []
44 soup = BeautifulSoup(data,'lxml') 45 div = soup.find('div',{'class' : 'forecast clearfix'}) 46
47 air_quality = div.find('strong',class_='level_2').string #空气质量
48 date = div.find('a',href='https://tianqi.moji.com/today/china/shandong/penglai').string 49 wind_direction = div.find('em').string #风向
50 wind_grade = div.find('b').string #风速
51 ul = div.find('ul',{'class' : 'clearfix'}) 52
53 ## 天气状况抽取 ##
54 a = [] 55 li = ul.find_all('li') 56 j=0 57 #return li
58 for i in li: 59 j+=1
60 if j==2: 61 a = i 62 a = str(a).replace('\n','').replace('\t','').replace(' ','').replace("</li>","").replace('\r','') 63 a = a.replace('<li><span>','').replace('<imgalt=','').replace('src="https://h5tq.moji.com/tianqi/assets/images/weather/','') 64 a = a.replace('.png/></span>','').replace('.png"/></span>','').replace('"','').replace('\t','') 65
66 for x in range(100,-1,-1): 67 #print("w{0}".format(x))
68 a = a.replace(("w{0}".format(x)),'') 69
70 if(len(a)==2): 71 a = a[0:1] 72 if(len(a)==4): 73 a = a[0:2] 74 #print(a)
75
76 for day in li: 77 if not isinstance(day,bs4.element.Tag): 78 date = day.find('a',href='https://tianqi.moji.com/today/china/shandong/penglai').string 79
80 weather_list.append(day.string) 81 if not isinstance(day,bs4.element.Tag): 82 wind_direction = day.find('em').string 83 wind_grade = day.find('b').string 84
85 Tempreture = weather_list[2] 86 air_quality =air_quality.replace("\n","").replace(' ','') 87 #air_quality = str(air_quality).replace('\n','').replace(' ','')
88 #print("data {0} Tempreture {1}".format(date,Tempreture))
89 return (" 时 间 : {}\n 天气状况: {}\n 温 度 : {}\n 风 向 : {}\n 风 速 : {}\n 空气质量: {}\n".format(date,a,Tempreture,wind_direction,wind_grade,air_quality)) 90
91 def send_email(email): 92 '''
93 sender = input('From: ') 94 password = input('password: ') 95 smtp_server = input('SMTP_Server: ') 96 '''
97
98 ## FROM ##
99 sender = 'wyl13****25@sohu.com'
100 sent_host = 'smtp.sohu.com'
101 sent_user = 'wyl13*****25@sohu.com'
102 sent_pass = '****密码****'
103
104 ## TO ##
105 receivers = ['13****25@qq.com','2******09@qq.com'] 106
107 #message = MIMEText("亲爱的,我如今来播报今天的蓬莱天气。\n(嗯...其实如今还只能看不能播)以下所示:\n 时 间| 天气状况 | 温 度 | 风 向 | 风 速 | 空气质量\n'{0}\n".format(result),'plain','utf-8')
108 message = MIMEText("亲爱的今天蓬莱天气是这样的呦 :\n{}\n".format(result),'plain','utf-8') 109
110 ## JUST USE TO DISPLAY ##
111 message['From'] = Header('树莓派(From)','utf-8') 112 message['To'] = Header('win10(To)','utf-8') 113 Subject = "今天的天气预报详情,来自树莓派!"
114 message['Subject'] = Header(Subject,'utf-8') #标题
115 try: 116 server = smtplib.SMTP() 117 #server = smtplib.SMTP(sent_host,25)
118 print("SMTP complete") 119
120 server.connect(sent_host,25) 121 print("connect complete") 122
123 #server.set_debuglevel(1)
124 server.login(sent_user,sent_pass) 125 print("login complete") 126
127 server.sendmail(sender,receivers[0],message.as_string()) 128 print("邮件发送成功") 129 #server.quit()
130
131 except smtplib.SMTPException: 132 print("Error:发生未知错误,没法发送邮件!") 133
134 if __name__ == '__main__': 135 result =[] 136 #win10_url = 'http://tianqi.moji.com/weather/china/shandong/penglai'
137 url = 'https://tianqi.moji.com/weather/china/shandong/penglai'
138 data = get_content(url) 139 result = get_weather(url,data) 140 result = str(result).replace("\\r","").replace('\t','').replace('\r','').replace("\\n","") 141 print("result is \n{}\n".format(result)) 142 #send_email(result)
最后测试结果以下:
你们应该也看到了有个很明显的BUG就是晴晴,
可是通过个人测试,在python3下彻底ok,毕竟3才是将来的霸主,2就不要花太多心思了。
我上面也有pip3的教程,安装好了,把咱们所需的库安装到位,打开方式切换为python3IDLE不然默认都是python2IDLE
并且。。。并且python2中也没有运行结果没有输出,也许是由于python2 和 python3 的输出函数不同所致,
python2 中print 没有括号,python3中更加严谨规范带有括号。
终于兑现了,舒心了很多....整理的不够到位的地方还请指正。
过一阵子再给你们更新一个带语音播报的邮件出来?我也不知道没想好,会分享给你们,敬请期待吧。
固然这篇教程只是用来学习,请勿进行商业活动甚至非法活动,切勿侵害他人权益,提早声明概不负责。
若是以为个人文章还不错,关注一下,顶一下 ,我将会用心去创做更好的文章,敬请期待。