導語
綠植之於家居的地位可以說是越來越重要,也是最容易出效果的軟裝物料了吧!
不過,不同於其他的軟裝選手,綠植擁有更柔軟的生命,需要你花心思呵護。那麼問題又來了,總是把植物養死怎麼破?
之前一直有養一些小植物的小興趣,但是每次都養不活,除了綠蘿,哈哈哈!養過的玫瑰花、梔子花帶著花苞買回來幾天就涼了!23333~
這不,我不死心啊,最近入手了一小盆的多肉綠植。【聽說多肉好養我才買的!】這盆是藍鳥多肉植物哦!顏色特別好看~顏值高的一眼相中了。
就是不知道在我手裡這盆多肉能撐幾天?也不說為了養活這盆植物,至少要上心嘛~所以我小心小心地準備開始養了。
但是不知道多肉植物的習慣,光照等等——
SO 做了一款Python介面化小程式—多肉植物的資料查詢小系統,養護的日照、施肥、繁殖等等都在裡面!
這次肯定養的活了哈!家裡有種植多肉植物的這次可有福利啦,跟著小編一起吧~
正文
本文是基於Pyqt5的介面化小程式哦,首先是把環境都安裝好啦!
(1)環境安裝:Python3、Pycharm安裝包;PyQt5、pillow、requests模組以及一些自帶的模組。
模組安裝統使用豆瓣的映象源安裝哈:pip install -i https://pypi.douban.com/simple/ +模組名。
(2)選擇的多肉資料網址是這個:http://www.mengsang.com/duorou/需要先獲取這些資料。程式碼如下:
'''
Function:
多肉資料爬蟲
'''
import os
import time
import random
import pickle
import requests
from lxml import etree
'''多肉資料爬蟲'''
class SucculentCrawler():
def __init__(self, **kwargs):
self.referer_list = ["http://www.google.com/", "http://www.bing.com/", "http://www.baidu.com/", "https://www.360.cn/"]
self.ua_list = ['Mozilla/5.0 (Linux; Android 5.1.1; Z828 Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.111 Mobile Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
'Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) CriOS/47.0.2526.107 Mobile/12F69 Safari/600.1.4',
'Mozilla/5.0 (iPad; CPU OS 11_2_5 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) CriOS/64.0.3282.112 Mobile/15D60 Safari/604.1',
'Mozilla/5.0 (Linux; Android 7.1.1; SM-T350 Build/NMF26X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.111 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.98 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36',
'Mozilla/5.0 (Linux; Android 6.0.1; SM-G610F Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
'Mozilla/5.0 (Linux; Android 5.1.1; 5065N Build/LMY47V; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/46.0.2490.76 Mobile Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36']
self.page_urls = self.__getAllPageUrls()
self.page_pointer = -1
self.savedir = 'resources/succulents'
'''爬取下一頁資料'''
def next(self):
# 獲取連結
self.page_pointer += 1
if self.page_pointer >= len(self.page_urls):
return True
page_url = self.page_urls[self.page_pointer]
# 提取該頁中多肉的圖片+詳情頁連結
res = requests.get(page_url, headers=self.__randomHeaders())
res.encoding = 'gbk'
html = etree.HTML(res.text)
html = html.xpath('//span[@class="tImgUlImg"]')
succulent_list = []
for item in html:
succulent_list.append([item.xpath('a/@title')[0].replace('/', '-').replace('\\', '-'), item.xpath('a/img/@src')[0], item.xpath('a/@href')[0]])
# 爬取詳情頁資料
for item in succulent_list:
data = [item[0], item[1]]
headers = self.__randomHeaders()
headers.update({'Referer': page_url})
res = requests.get(item[-1], headers=headers)
res.encoding = 'gbk'
html_root = etree.HTML(res.text).xpath('//div[@class="cbRight"]/div[@class="mainBox"]')[0]
html = html_root.xpath('div[2]/table[@class="tTable"]/tr')[1:]
intro = ['繁殖: ', '易活度: ', '季節: ', '溫度: ', '日照: ', '澆水量: ',
'日照說明: ', '澆水說明: ', '大類/屬: ', '中文種名: ', '英文學名: ']
for idx, tr in enumerate(html):
if idx == 0:
intro[0] = intro[0] + tr.xpath('./td[2]/text()')[0] if tr.xpath('./td[2]/text()') else intro[0] + '未知'
intro[1] = intro[1] + int(tr.xpath('./td[4]/img/@src')[0].split('/')[-1].split('.')[0][1:]) * ''
elif idx == 1:
intro[2] = intro[2] + tr.xpath('./td[2]/text()')[0] if tr.xpath('./td[2]/text()') else intro[2] + '未知'
intro[3] = intro[3] + tr.xpath('./td[4]/text()')[0].strip().replace(' ', '') if tr.xpath('./td[4]/text()') else intro[3]
elif idx == 2:
intro[4] = intro[4] + int(tr.xpath('./td[2]/img/@src')[0].split('/')[-1].split('.')[0]) * ''
intro[5] = intro[5] + int(tr.xpath('./td[4]/img/@src')[0].split('/')[-1].split('.')[0][1:]) * ''
html = html_root.xpath('div[2]/div')[0].xpath('//div[@class="pt5"]')
for idx, item in enumerate(html):
if idx == 0:
intro[6] = intro[6] + item.xpath('./span/text()')[0]
elif idx == 1:
intro[7] = intro[7] + item.xpath('./span/text()')[0]
elif idx == 3:
intro[8] = intro[8] + item.xpath('text()')[0] if item.xpath('text()') else intro[8] + '未知'
elif idx == 4:
intro[9] = intro[9] + item.xpath('text()')[0] if item.xpath('text()') else intro[9] + '未知'
elif idx == 5:
intro[10] = intro[10] + item.xpath('text()')[0] if item.xpath('text()') else intro[10] + '未知'
data.append(intro)
self.__saveItem(data)
time.sleep(random.random())
return False
'''資料儲存'''
def __saveItem(self, data):
if not os.path.exists(self.savedir):
os.mkdir(self.savedir)
savepath = os.path.join(self.savedir, data[0])
if not os.path.exists(savepath):
os.mkdir(savepath)
f = open(os.path.join(savepath, 'show.jpg'), 'wb')
f.write(requests.get(data[1], headers=self.__randomHeaders()).content)
f.close()
f = open(os.path.join(savepath, 'info.pkl'), 'wb')
pickle.dump(data, f)
f.close()
'''獲得所有連結'''
def __getAllPageUrls(self):
res = requests.get('http://www.mengsang.com/duorou/list_1_1.html', headers=self.__randomHeaders())
res.encoding = 'gbk'
html = etree.HTML(res.text)
num_pages = html.xpath('//span[@class="pageinfo"]/strong')[0].text
page_urls = ['http://www.mengsang.com/duorou/list_1_%s.html' % i for i in range(1, int(num_pages)+1)]
return page_urls
'''隨機請求頭'''
def __randomHeaders(self):
return {'user-agent': random.choice(self.ua_list), 'referer': random.choice(self.referer_list)}
(3)系統介面設定。
class SucculentQuery(QWidget):
def __init__(self, parent=None, **kwargs):
super(SucculentQuery, self).__init__(parent)
self.setWindowTitle('多肉植物——資料查詢系統')
self.setWindowIcon(QIcon('resources/icon/icon.png'))
# 定義元件
self.label_name = QLabel('多肉名稱: ')
self.line_edit = QLineEdit()
self.button_find = QPushButton()
self.button_find.setText('查詢')
self.label_result = QLabel('查詢結果:')
self.show_label = QLabel()
self.show_label.setFixedSize(300, 300)
self.showLabelImage('resources/icon/icon.png')
self.text_result = QTextEdit()
self.button_random = QPushButton()
self.button_random.setText('隨機讀取')
self.button_update = QPushButton()
self.button_update.setText('資料更新')
self.tip_label = QLabel()
self.tip_label.setText('資料狀態: 未在更新資料, 資料更新進度: 0/0')
# 排版
self.grid = QGridLayout()
self.grid.addWidget(self.label_name, 0, 0, 1, 1)
self.grid.addWidget(self.line_edit, 0, 1, 1, 30)
self.grid.addWidget(self.button_find, 0, 31, 1, 1)
self.grid.addWidget(self.button_random, 0, 32, 1, 1)
self.grid.addWidget(self.button_update, 0, 33, 1, 1)
self.grid.addWidget(self.tip_label, 1, 0, 1, 31)
self.grid.addWidget(self.label_result, 2, 0)
self.grid.addWidget(self.text_result, 3, 0, 1, 34)
self.grid.addWidget(self.show_label, 3, 34, 1, 1)
self.setLayout(self.grid)
self.resize(600, 400)
# 事件繫結
self.button_find.clicked.connect(self.find)
self.button_random.clicked.connect(self.randomRead)
self.button_update.clicked.connect(lambda _: threading.Thread(target=self.update).start())
效果如下所示——
總結
想讓多肉植物正常長大,一方面要保證根系是好的,另外一方面要少量施肥,不要乾旱得太厲害。
尤其秋冬給肉肉控狀態的時候,長期不澆水老葉子消耗比較多,葉片聚攏,新葉遲遲長不出,雖然顏色出來了,但肉肉會明顯變小啦~啥時候我養的綠植成這樣子那就真的成功了!
完整的免費原始碼領取處:
如需完整的專案原始碼+素材原始碼基地見:#私信小編06#即可獲取免費的福利!
你們的支援是我最大的動力!!記得三連哦~mua 歡迎大家閱讀往期的文章哦~
推薦往期文章:
Python—2021 |已有文章彙總 | 持續更新,直接看這篇就夠了~