炭黑、維生素出廠價價格獲取且視覺化
阿新 • • 發佈:2018-11-19
#獲取了炭黑,維生素D3,維生素B1,維生素B2的出廠價變化情況趨勢
這是總的效果及程式碼,結合了tkinter視覺化介面操作
import requests from lxml import etree import re import matplotlib.pyplot as plt import matplotlib as mpl # 配置字型 import tkinter as tk window=tk.Tk() window.title("公司資訊查詢器") window.geometry("790x550+500+200") l1=tk.Label(window,text="價格變化結果:",font="微軟雅黑 11",height=2) l1.grid() e1=tk.Text(window,height=30) # e1=tk.Entry(window,textvariable=var,width=60,) e1.grid(row=2,column=1) def click(): mpl.rcParams["font.sans-serif"] = ["Microsoft YaHei"] #配置字型,不然漢字有的顯示不正常 date=[] price=[] for ii in range(1,6): url="http://tanhei.100ppi.com/kx/list---{}.html".format(ii) html=requests.get(url).text new_html=etree.HTML(html) content=new_html.xpath('//div[@class="pr-news-txt"]/text()') print(content) for i in range(len(content)): date1=re.findall('(.*?),炭黑',content[i])[0] price1=re.findall('參考價為(.*?)\.',content[i])[0] date.append(date1) price.append(int(price1)) date.reverse() #反轉/倒序排序 price.reverse() new=list(zip(date,price)) e1.insert("end", "炭黑:\n") # 換行騷操作 e1.insert("end", new) e1.insert("end", "\n\n") # 換行騷操作 plt.plot(date,price,'r*-') plt.xlabel("時間") plt.ylabel("元/每噸") plt.title('炭黑價格走勢') plt.show() def click1(): date = [] price_vd = [] price_vb1 = [] price_vb2 = [] mpl.rcParams["font.sans-serif"] = ["Microsoft YaHei"] # 配置字型,不然漢字有的顯示不正常 for ii in range(0, 3): url = "http://www.feedtrade.com.cn/e/search/result/index.php?page={}&searchid=58042".format(ii) html = requests.get(url).text # print(html) new_html = etree.HTML(html) content = new_html.xpath('//a[@class="l"]/@href') for i in range(len(content)): detil = requests.get(url=content[i]) detil.encoding = 'gbk' detil = detil.text # print(detil) new_detil = new_html = etree.HTML(detil) if new_detil.xpath('//td[@class="text"]//tr[4]/td[3]/text()')[0][-2:] == "公斤": date1 = new_detil.xpath('//h1/text()')[0] date2 = re.findall('年(.*?)北京', date1)[0] price1 = new_detil.xpath('//td[@class="text"]//tr[4]/td[3]/text()')[0] price2 = new_detil.xpath('//td[@class="text"]//tr[5]/td[3]/text()')[0] price3 = new_detil.xpath('//td[@class="text"]//tr[6]/td[3]/text()')[0] date.append(date2) price_vd.append(int(price1[0:3])) price_vb1.append(int(price2[0:3])) price_vb2.append(int(price3[0:3])) else: date1 = new_detil.xpath('//h1/text()')[0] date2 = re.findall('年(.*?)北京', date1)[0] price1 = new_detil.xpath('//td[@class="text"]//tr[4]/td[4]/text()')[0] price2 = new_detil.xpath('//td[@class="text"]//tr[5]/td[4]/text()')[0] price3 = new_detil.xpath('//td[@class="text"]//tr[6]/td[4]/text()')[0] date.append(date2) price_vd.append(int(price1[0:3])) price_vb1.append(int(price2[0:3])) price_vb2.append(int(price3[0:3])) date.reverse() # 反轉/倒序排序 price_vd.reverse() price_vb1.reverse() price_vb2.reverse() new1=list(zip(date, price_vd)) new2 =list(zip(date, price_vb1)) new3 =list(zip(date, price_vb2)) e1.insert("end", "維生素D3:\n") # 換行騷操作 e1.insert("end", new1) e1.insert("end", "\n\n") # 換行騷操作 e1.insert("end", "維生素B1:\n") # 換行騷操作 e1.insert("end", new2) e1.insert("end", "\n\n") # 換行騷操作 e1.insert("end", "維生素B2:\n") # 換行騷操作 e1.insert("end", new3) e1.insert("end", "\n\n") # 換行騷操作 fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax3 = fig.add_subplot(2, 2, 3) ax4 = fig.add_subplot(2, 2, 4) ax1.plot(date, price_vd, 'r*-') ax1.set_title('VD價格走勢') ax3.plot(date, price_vb1, 'g*-') ax3.set_title('VB1價格走勢') ax4.plot(date, price_vb2, 'b*-') ax4.set_title('VB2價格走勢') plt.show() b=tk.Button(window,text="炭黑(元/頓)",command=click,width=10,font="微軟雅黑 12") b.grid(row=6,column=0) b2=tk.Button(window,text="維生素B/D(元/公斤)",command=click1,width=16,font="微軟雅黑 12") b2.grid(row=6,column=1) b1=tk.Button(window,text="退出",command=window.quit,width=10,font="微軟雅黑 12") b1.grid(row=6,column=2) window.mainloop()
下面是開始分開寫的兩端程式,上面是合併在一起的效果
1.炭黑
import requests from lxml import etree import re import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl # 配置字型 mpl.rcParams["font.sans-serif"] = ["Microsoft YaHei"] #配置字型,不然漢字有的顯示不正常 date=[] price=[] for ii in range(1,6): url="http://tanhei.100ppi.com/kx/list---{}.html".format(ii) html=requests.get(url).text new_html=etree.HTML(html) content=new_html.xpath('//div[@class="pr-news-txt"]/text()') print(content) for i in range(len(content)): date1=re.findall('(.*?),炭黑',content[i])[0] price1=re.findall('參考價為(.*?)\.',content[i])[0] date.append(date1) price.append(int(price1)) date.reverse() #反轉/倒序排序 price.reverse() print(list(zip(date,price))) plt.plot(date,price,'r*-') plt.xlabel("時間") plt.ylabel("元/每噸") plt.title('炭黑價格走勢') plt.show()
2.維生素
import requests from lxml import etree import re import pandas as pd import matplotlib.pyplot as plt import matplotlib as mpl # 配置字型 date=[] price_vd=[] price_vb1=[] price_vb2=[] mpl.rcParams["font.sans-serif"] = ["Microsoft YaHei"] #配置字型,不然漢字有的顯示不正常 for ii in range(0,3): url="http://www.feedtrade.com.cn/e/search/result/index.php?page={}&searchid=58042".format(ii) html=requests.get(url).text # print(html) new_html=etree.HTML(html) content=new_html.xpath('//a[@class="l"]/@href') for i in range(len(content)): detil=requests.get(url=content[i]) detil.encoding='gbk' detil=detil.text # print(detil) new_detil=new_html=etree.HTML(detil) if new_detil.xpath('//td[@class="text"]//tr[4]/td[3]/text()')[0][-2:] =="公斤": date1=new_detil.xpath('//h1/text()')[0] date2=re.findall('年(.*?)北京',date1)[0] price1=new_detil.xpath('//td[@class="text"]//tr[4]/td[3]/text()')[0] price2=new_detil.xpath('//td[@class="text"]//tr[5]/td[3]/text()')[0] price3=new_detil.xpath('//td[@class="text"]//tr[6]/td[3]/text()')[0] date.append(date2) price_vd.append(int(price1[0:3])) price_vb1.append(int(price2[0:3])) price_vb2.append(int(price3[0:3])) else: date1 = new_detil.xpath('//h1/text()')[0] date2 = re.findall('年(.*?)北京', date1)[0] price1 = new_detil.xpath('//td[@class="text"]//tr[4]/td[4]/text()')[0] price2 = new_detil.xpath('//td[@class="text"]//tr[5]/td[4]/text()')[0] price3 = new_detil.xpath('//td[@class="text"]//tr[6]/td[4]/text()')[0] date.append(date2) price_vd.append(int(price1[0:3])) price_vb1.append(int(price2[0:3])) price_vb2.append(int(price3[0:3])) date.reverse() #反轉/倒序排序 price_vd.reverse() price_vb1.reverse() price_vb2.reverse() print(list(zip(date,price_vd))) print(list(zip(date,price_vb1))) print(list(zip(date,price_vb2))) fig=plt.figure() ax1=fig.add_subplot(2,2,1) ax3=fig.add_subplot(2,2,3) ax4=fig.add_subplot(2,2,4) ax1.plot(date,price_vd,'r*-') ax1.set_title('VD價格走勢') ax3.plot(date,price_vb1,'g*-') ax3.set_title('VB1價格走勢') ax4.plot(date,price_vb2,'b*-') ax4.set_title('VB2價格走勢') plt.show()