#获取了炭黑,维生素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()