import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from pyquery import PyQuery as pq
import pymongo
'''链接mongodb'''
client=pymongo.MongoClient("mongodb://*********:27017")
db=client.test
col=db.taobao
'''连接mysql'''
db=pymysql.Connect(host='*********',user='root',password='*********',port=3306,db='xiaomishop')
cursor=db.cursor()
table='taobao'
'''初始url'''
kw='iPad' #关键词
url='https://s.taobao.com/search?q='+kw
driver=webdriver.Chrome() #指定浏览器对象
wait=WebDriverWait(driver,50) #最大等待时长
def Get_page(page_num):
'''
获取page_num页的页面信息(html)
:param page_num:
:return: Null
'''
print('='*25,'正在爬取第',page_num,'页','='*25)
try:
driver.get(url)
input = wait.until(EC.presence_of_element_located((By.CLASS_NAME,'J_Input'))) # 直到定位到搜索框
submit=wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'J_Submit'))) #直到定位到能点击‘确定’
input.clear()
input.send_keys(page_num)
submit.click()
Get_product()
except TimeoutError:
Get_page(page_num)
def Get_product():
'''
获取每页上所有商品的名称、价格、图片、购买量、店铺名称、店铺位置
:return:Null
'''
html=driver.page_source
doc=pq(html)
items=doc('.J_MouserOnverReq').items()
for item in items:
product={
'name': item.find('.ctx-box .row-2 a').text(),
'pic_img':item.find('.pic-box-inner .J_Itemlist_Pic_657878599145').attr('src'),
'price':item.find('.ctx-box .row-1 strong').text(),
'nums':item.find('.ctx-box .row-1 .deal-cnt').text(),
'shop':item.find('.ctx-box .row-3 .shop span').text(),
'location':item.find('.ctx-box .row-3 .location').text()
}
# write_to_mongodb(product)
# write_to_mysql(product)
print(product)
def write_to_mongodb(dic):
'''
字典结果保存到MongoDB
:param dic:
:return: Null
'''
col.insert_one(dic)
def write_to_mysql(dic):
'''
字典结果写入mysql
:param dic:
:return:
'''
keys=','.join(dic.keys())
values=','.join(['%s']*len(dic))
sql='insert into {table}({keys}) values ({values})'.format(table=table,keys=keys,values=values)
try:
if cursor.execute(sql,tuple(dic.values())):
print('successful')
db.commit()
except:
print('failed')
db.rollback()
if __name__ == '__main__':
for page_num in range(1,101):
Get_page(page_num)
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
'''基本使用'''
#声明浏览器对象
browser=webdriver.Chrome()
try:
'''访问页面'''
browser.get('https://www.baidu.com')
input=browser.find_element(By.ID,'kw')
input.send_keys('python')
input.send_keys(Keys.ENTER)
wait=WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,'content_left')))
print('='*50)
print(browser.current_url)
print('=' * 50)
print(browser.get_cookies())
print('=' * 50)
# print(browser.page_source)
browser.quit()
except:
browser.close()
'''节点交互'''
driver=webdriver.Chrome()
try:
driver.get('https://www.taobao.com')
input=driver.find_element(By.ID,'q')
input.send_keys('手机')
# input.clear() #清除
button=driver.find_element(By.CLASS_NAME,'btn-search')
button.click()
driver.close()
except:
driver.close()
'''动作链'''
driver=webdriver.Chrome()
try:
driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
driver.switch_to.frame('iframeResult') #切换frame
source=driver.find_element(By.ID,'draggable')
target=driver.find_element(By.ID,'droppable')
actions=ActionChains(driver)
actions.drag_and_drop(source,target)
actions.perform()
except:
driver.close()
'''执行JavaScript,比如下拉进度条'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
driver.execute_script('alert("To Bottom")')
except:
print('error')
driver.close()
'''获取属性'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
logo=driver.find_element(By.ID,'special')
print(logo)
print(logo.get_attribute('class'))
driver.close()
except:
print('error')
driver.close()
'''获取文本'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
res=driver.find_element(By.CLASS_NAME,'ExploreSpecialCard-contentTitle')
print(res.text)
driver.close()
except:
print('error')
driver.close()
'''获取id、位置、标签吗和大小'''
driver=webdriver.Chrome()
try:
driver.get('https://www.zhihu.com/explore')
res=driver.find_element(By.CLASS_NAME,'ExploreSpecialCard-contentTitle')
print('id',res.id)
print('位置',res.location)
print('标签',res.tag_name)
print('大小',res.size)
driver.close()
except:
print('error')
driver.close()
'''延时显式等待'''
driver=webdriver.Chrome()
try:
driver.get('https://www.taobao.com')
#引入WebDriverWait对象,设置最长等待时间
wait=WebDriverWait(driver,10)
#调用until方法,传入等待条件;EC.presence_of_element_located代表节点出现
input=wait.until(EC.presence_of_element_located((By.ID,'q')))
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search')))
print(input,button)
driver.close()
except:
print('error')
driver.close()
'''前进和后退'''
driver=webdriver.Chrome()
try:
driver.get('https://www.taobao.com/')
driver.get('https://www.baidu.com/')
driver.get('https://www.python.org/')
driver.back() #后退
time.sleep(1)
driver.forward() #前进
driver.close()
except:
print('error')
driver.close()
import pymysql
from pyquery import PyQuery as pq
import requests
def Get_HTML(url):
headers = {'User-Agent': 'Mozilla/5.0'}
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
print("Error!")
def parse_page(html):
doc=pq(html)
title=doc('.li03 a').text().split(' ') #名称
actor=doc('.li04 span').text().split(' ') #演员表
index=doc('.li01 b').text().split(' ') #序号
href=[] #链接
for i in doc('.li03 a').items(): #遍历
href.append(i.attr('href'))
score=doc('.li05 span').text().split(' ') #评分
#保存为字典
Result={}
for i in range(100):
result={
'序号':index[i],
'名称':title[i],
'链接':href[i],
'演员':actor[i],
'评分':score[i]
}
Result[index[i]]=result
return Result
def write_to_mysql(dict):
db=pymysql.connect(host='*******',user='root',password='******',passwd='3306',db='*******')
cursor=db.cursor()
table='movie' #表名
keys=','.join(dict.keys())
values=','.join(['%s']*len(dict))
sql='insert into {table}({keys}) values ({values})'.format(table=table,keys=keys,values=values)
try:
if cursor.execute(sql,tuple(dict.values())):
print("successful")
db.commit()
except:
print("failed")
db.rollback()
db.close()
#若主键存在,进行更新操作;否则进行插入数据
def update_data(dict):
db = pymysql.connect(host='*******',user='root',password='******',passwd='3306',db='*******')
cursor=db.cursor()
table='movie' #表名
keys=','.join(dict.keys())
values=','.join(['%s']*len(dict))
sql='insert into {table}({keys}) values ({values}) ON DUPLICATE KEY UPDATE'.format(table=table,keys=keys,values=values)
update=','.join([' {key}=%s'.format(key=key) for key in dict])
sql+=update
try:
if cursor.execute(sql,tuple(dict.values())*2):
print("successful")
db.commit()
except:
print("failed")
db.rollback()
db.close()
#查询
def search():
db = pymysql.connect(host='*******',user='root',password='******',passwd='3306',db='*******')
cursor = db.cursor()
sql = 'select * from movie'
try:
cursor.execute(sql)
print(cursor.rowcount)
row = cursor.fetchone()
while row:
print('Row:', row)
row = cursor.fetchone()
except:
print("Error")
db.close()
def main():
url = 'https://www.1905.com/vod/top/lst/'
html = Get_HTML(url)
item=parse_page(html)
for i in range(len(item)):
write_to_mysql(item[str(i+1)])
main()
import requests
import json
import re
import pandas as pd
import time
from pyecharts.components import Table
from pyecharts import options as opts
from pyecharts.charts import Bar, Page,Pie,Line,Timeline,Map
from pyecharts.commons.utils import JsCode
from pyecharts.options import DataZoomOpts,ComponentTitleOpts
def Get_HTML():
headers = {'User-Agent': 'Mozilla/5.0'}
url = 'https://m.look.360.cn/events/feiyan?sv=&version=&market=&device=2&net=4&stype=&scene=&sub_scene=&refer_scene=&refer_subscene=&f=jsonp&location=true&sort=2&_=1649252949072&callback=jsonp2'
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
re = r.text[7:-2]
response = json.loads(re)
return response
except:
print("error!")
def China_data_Total():
url='http://m.look.360.cn/subject/400?sign=360_6aa05217'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0'}
html = requests.get(url, headers=headers).text
pattern=re.compile('"qcmsint1":"0"},"feiyanTrend":(.*),"local":',re.S)
item=re.findall(pattern,html)
response=json.loads(item[0])
data = {
"截止日期":response['total']['modifyTime'],
"现存确诊":response['total']['currentConfirmed'],
"境外输入":response['total']['overseasInput'],
"现存无症状":response['total']['asymptom'],
"累计确诊":response['total']['diagnosed'],
"累计治愈":response['total']['cured'],
"累计死亡":response['total']['died'],
"现存_较昨日":response['newDiff']['currentConfirmed'],
"境外输入_较昨日":response['newDiff']['overseasInput'],
"无症状_较昨日":response['newDiff']['asymptom'],
"累计_较昨日":response['newDiff']['diagnosed'],
"治愈_较昨日":response['newDiff']['cured'],
"死亡_较昨日":response['newDiff']['died']
}
return data
# 国内数据
def China_data(response):
P_name, P_Ljqz, P_Cured, P_Died, P_Xcqz, P_Xzqz = [], [], [], [], [], [] #省数据
C_name, C_Ljqz, C_Cured, C_Died, C_Xcqz, C_Xzqz = [], [], [], [], [], [] #市数据
for i in range(34):
P_name.append(response['data'][i]['cityShortName']) # 城市名称
P_Ljqz.append(response['data'][i]['diagnosed']) # 累计确诊
P_Cured.append(response['data'][i]['cured']) # 治愈
P_Died.append(response['data'][i]['died']) # 死亡
P_Xcqz.append(response['data'][i]['currentConfirmed']) # 现存确诊
P_Xzqz.append(response['data'][i]['diffDiagnosed']) # 新增确诊
data = {
"地区": P_name,
"新增确诊": P_Xzqz,
"现存确诊": P_Xcqz,
"累计确诊": P_Ljqz,
"治愈人数": P_Cured,
"死亡人数": P_Died,
}
return data
#河南省数据
def Henan_data():
url = 'https://m.look.360.cn/events/feiyanCityInfo?sv=&version=&market=&device=2&net=4&stype=&scene=&sub_scene=&refer_scene=&refer_subscene=&f=jsonp&ename=henan&_=1650426134574&callback=jsonp4'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0'}
html = requests.get(url, headers=headers).text[7:-2]
response=json.loads(html)
# print(response['data']['cityInfo'])
C_name, C_Ljqz, C_Cured, C_Died, C_Xcqz, C_Xzqz = [], [], [], [], [], [] # 市数据
for i in range(len(response['data']['cityInfo'])):
C_name.append(response['data']['cityInfo'][i]['cityName']) #名称
C_Ljqz.append(response['data']['cityInfo'][i]['diagnosed']) # 累计确诊
C_Cured.append(response['data']['cityInfo'][i]['cured']) # 治愈
C_Died.append(response['data']['cityInfo'][i]['died']) # 死亡
C_Xzqz.append(response['data']['cityInfo'][i]['diffDiagnosed']) # 新增确诊
data = {
"地区": C_name,
"新增确诊": C_Xzqz,
"累计确诊": C_Ljqz,
"治愈人数": C_Cured,
"死亡人数": C_Died
}
return data
def Abroad_data(response): # 国外数据
C_name, Ljqz, Cured, Died, Xcqz, Xzqz = [], [], [], [], [], []
for i in range(196):
c_name = response['country'][i]['provinceName'] # 城市名称
ljqz = response['country'][i]['diagnosed'] # 累计确诊
cured = response['country'][i]['cured'] # 治愈
died = response['country'][i]['died'] # 死亡
xzqz = response['country'][i]['diffDiagnosed'] # 新增确诊
C_name.append(c_name)
Ljqz.append(ljqz)
Cured.append(cured)
Died.append(died)
Xzqz.append(xzqz)
data = {
"地区": C_name,
"新增确诊": Xzqz,
"累计确诊": Ljqz,
"治愈人数": Cured,
"死亡人数": Died
}
return data
def TimeLine():
headers = {'User-Agent': 'Mozilla/5.0'}
url = 'https://api.look.360.cn/events/feiyanHomeMulTrend?sv=&version=&market=&device=2&net=4&stype=&scene=&sub_scene=&refer_scene=&refer_subscene=&f=jsonp&_=1650032782170&callback=jsonp3'
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
re = r.text[7:-2]
response = json.loads(re)
except:
print("error!")
time,Ljqz, Cured, Died, Xcqz, Xzqz, Xz_Died, Xz_Cured= [], [], [], [], [], [], [], []
for i in range(len(response['data']['timeline_n1'])):
time.append(response['data']['timeline_n1'][i]['time']) #时间
Xzqz.append(response['data']['timeline_n1'][i]['diagnosed']) #新增确诊
Xcqz.append(response['data']['timeline_n2'][i]['total_currentConfirmed']) #现存确诊
Ljqz.append(response['data']['timeline_n2'][i]['total_diagnosed']) #累计确诊
Cured.append(response['data']['timeline_n3'][i]['total_cured']) #累计治愈
Died.append(response['data']['timeline_n3'][i]['total_died']) #累计死亡
Xz_Died.append(response['data']['timeline_n4'][i]['died']) #新增死亡
Xz_Cured.append(response['data']['timeline_n4'][i]['cured']) #新增治愈
data={
"时间":time,
"新增确诊":Xzqz,
"现存确诊":Xcqz,
"累计确诊":Ljqz,
"累计治愈":Cured,
"累计死亡":Died,
"新增死亡":Xz_Died,
"新增治愈":Xz_Cured
}
return data
def View(data_1,data_2,data_3,data_4):
#柱状图
c_1 = Timeline(init_opts=opts.InitOpts(chart_id='1'))
c_1.add_schema(play_interval=1000,
label_opts=opts.series_options.LabelOpts(color='#cfe2f3'),is_auto_play = True)
for i in range(3):
#全国累计详情柱状图
C_1 = (
Bar()
.add_xaxis(data_1['地区'][i*11:(i+1)*11])
.add_yaxis("累计确诊", data_1['累计确诊'][i*11:(i+1)*11],stack='stack1',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("新增确诊", data_1['新增确诊'][i*11:(i+1)*11],stack='stack1',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("死亡人数", data_1['死亡人数'][i * 11:(i + 1) * 11],stack='stack1',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("治愈人数", data_1['治愈人数'][i * 11:(i + 1) * 11],stack='stack1',label_opts=opts.LabelOpts(is_show=False))
.add_yaxis("现存确诊", data_1['现存确诊'][i * 11:(i + 1) * 11],stack='stack1',label_opts=opts.LabelOpts(is_show=False))
.reversal_axis()
# .set_global_opts(legend_opts=opts.LegendOpts(selected_mode='single')) # 单选模式
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color='white')),#调整x轴字体颜色
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color='white')),
legend_opts=opts.LegendOpts(selected_mode='single'),
title_opts=opts.TitleOpts("国内疫情详情",pos_right=0,pos_top=20,title_textstyle_opts=opts.TextStyleOpts(font_size=13,color="#cfe2f3")))
)
c_1.add(C_1, "第{}页".format(i+1))
# 全国地图
c_2 = (
Map(init_opts=opts.InitOpts(chart_id='2'))
# .add("死亡人数", [list(z) for z in zip(data_1['地区'], data_1['死亡人数'])], "china",is_selected=False) #默认关闭该标签
.add("累计确诊",[list(z) for z in zip(data_1['地区'], data_1['累计确诊'])], "china")
.add("死亡人数", [list(z) for z in zip(data_1['地区'], data_1['死亡人数'])], "china")
.add("治愈人数", [list(z) for z in zip(data_1['地区'], data_1['治愈人数'])], "china")
.add("新增确诊", [list(z) for z in zip(data_1['地区'], data_1['新增确诊'])], "china")
.add("现存确诊", [list(z) for z in zip(data_1['地区'], data_1['现存确诊'])], "china")
.set_global_opts(
title_opts=opts.TitleOpts(""),
visualmap_opts=opts.VisualMapOpts(max_=10000),
legend_opts=opts.LegendOpts(selected_mode='single') #单选模式
)
)
#时间线折线图
c_3 = (
Line(init_opts=opts.InitOpts(chart_id='3'))
.add_xaxis(data_3['时间'])
.add_yaxis("新增确诊",data_3['新增确诊'],label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='#DC143C'))
.add_yaxis("现存确诊",data_3['现存确诊'],label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='#a98175'))
.add_yaxis("累计确诊",data_3['累计确诊'], label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='#60281e'))
.add_yaxis("累计治愈",data_3['累计治愈'], label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='#2add9c'))
.add_yaxis("累计死亡",data_3['累计死亡'], label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='black'))
.add_yaxis("新增死亡",data_3['新增死亡'], label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='#758a99'))
.add_yaxis("新增治愈",data_3['新增治愈'], label_opts=opts.LabelOpts(is_show=False),linestyle_opts=opts.LabelOpts(color='#1bd1a5'))
.set_global_opts(
title_opts=opts.TitleOpts(title="疫情汇总时间线",pos_top=20,title_textstyle_opts=opts.TextStyleOpts(font_size=13,color="#cfe2f3")),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color='white')), # 调整x轴字体颜色
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(color='white')),
legend_opts=opts.LegendOpts(selected_mode='single')
)
)
#河南省地图
c_4 = (
Map(init_opts=opts.InitOpts(chart_id='4'))
# .add("死亡人数", [list(z) for z in zip(data_1['地区'], data_1['死亡人数'])], "china",is_selected=False) #默认关闭该标签
.add("累计确诊", [list(z) for z in zip(data_4['地区'], data_4['累计确诊'])], "河南")
.add("死亡人数", [list(z) for z in zip(data_4['地区'], data_4['死亡人数'])], "河南")
.add("治愈人数", [list(z) for z in zip(data_4['地区'], data_4['治愈人数'])], "河南")
.add("新增确诊", [list(z) for z in zip(data_4['地区'], data_4['新增确诊'])], "河南")
.set_global_opts(
title_opts=opts.TitleOpts(title='省内详细信息',pos_top=30,title_textstyle_opts=opts.TextStyleOpts(font_size=13, color="#cfe2f3")),
visualmap_opts=opts.VisualMapOpts(max_=500),
legend_opts=opts.LegendOpts(selected_mode='single') # 单选模式
)
)
#省内死亡详情饼图
c_5 = (
Pie(init_opts=opts.InitOpts(chart_id='5'))
.add("死亡人数", [list(z) for z in zip(data_4['地区'], data_4['死亡人数'])])
.set_global_opts(title_opts=opts.TitleOpts('市死亡人数',title_textstyle_opts=opts.TextStyleOpts(font_size=13, color="gray"),),
legend_opts=opts.LegendOpts(is_show=False))
# .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"),)
)
# 汇总数据与昨日相比
data = China_data_Total()
c_7 = (
Pie(init_opts=opts.InitOpts(chart_id='7'))
.set_global_opts(
title_opts=opts.TitleOpts(
title="现存确诊" + " " * 17 + "境外输入" + " " * 17 + "现存无症状", title_textstyle_opts=opts.TextStyleOpts(font_size=10, color="white"), ))
)
c_8 = (
Pie(init_opts=opts.InitOpts(chart_id='8'))
.set_global_opts(
title_opts=opts.TitleOpts(
title="累计确诊"+ " " * 17 + "累计治愈"+ " " * 17 + "累计死亡", title_textstyle_opts=opts.TextStyleOpts(font_size=10, color="white"), ))
)
c_9 = (
Pie(init_opts=opts.InitOpts(chart_id='9'))
.set_global_opts(
title_opts=opts.TitleOpts(
title=str(data['现存_较昨日']) + " " * 25 + str(data['境外输入_较昨日']) + " " * 25 + str(data['无症状_较昨日']), title_textstyle_opts=opts.TextStyleOpts(font_size=10, color="#e9e7ef"), ))
)
c_10 = (
Pie(init_opts=opts.InitOpts(chart_id='10'))
.set_global_opts(
title_opts=opts.TitleOpts(
title=str(data['累计_较昨日']) + " " * 25 + str(data['治愈_较昨日']) + " " * 25+ str(data['死亡_较昨日']), title_textstyle_opts=opts.TextStyleOpts(font_size=10, color="#e9e7ef")))
)
#数字
c_11 = (
Pie(init_opts=opts.InitOpts(chart_id='16'))
.set_global_opts(
title_opts=opts.TitleOpts(
title=str(data['现存确诊']) + " " * 3 + str(data['境外输入']) + " " * 3 + str(data['现存无症状']),
title_textstyle_opts=opts.TextStyleOpts(font_size=23, color="#CD3700")))
)
c_12 = (
Pie(init_opts=opts.InitOpts(chart_id='17'))
.set_global_opts(
title_opts=opts.TitleOpts(
title=str(data['累计确诊']) + " " * 3 + str(data['累计治愈']) + " " * 3 + str(data['累计死亡']),
title_textstyle_opts=opts.TextStyleOpts(font_size=23, color="#7F7F7F")))
)
tu_1 = (
Line(init_opts=opts.InitOpts(width="1500px",
height="850px",
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"},
chart_id='11'))
.add_xaxis([None])
.add_yaxis("", [None])
.set_global_opts(
title_opts=opts.TitleOpts(),
yaxis_opts=opts.AxisOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False))
)
tu_1.add_js_funcs(
"""
var img = new Image(); img.src = './images/bg_body.jpg';
"""
)
tu_2 = (
Line(init_opts=opts.InitOpts(
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"},
chart_id='12'))
.add_xaxis([None])
.add_yaxis("", [None])
.set_global_opts(
yaxis_opts=opts.AxisOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False))
)
tu_2.add_js_funcs(
"""
var img = new Image(); img.src = './images/bg_box5.png';
"""
)
tu_3 = (
Line(init_opts=opts.InitOpts(chart_id='13',
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"},
))
.add_xaxis([None])
.add_yaxis("", [None])
.set_global_opts(
yaxis_opts=opts.AxisOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False))
)
tu_3.add_js_funcs(
"""
var img = new Image(); img.src = './images/bg_box5.png';
"""
)
tu_4 = (
Line(init_opts=opts.InitOpts(chart_id='14',
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"},
))
.add_xaxis([None])
.add_yaxis("", [None])
.set_global_opts(
yaxis_opts=opts.AxisOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False))
)
tu_4.add_js_funcs(
"""
var img = new Image(); img.src = './images/bg_box4.png';
"""
)
tu_5 = (
Line(init_opts=opts.InitOpts(width="1500px",
height="200px",
bg_color={"type": "pattern", "image": JsCode("img"), "repeat": "no-repeat"},
chart_id='15'))
.add_xaxis([None])
.add_yaxis("", [None])
.set_global_opts(
title_opts=opts.TitleOpts(title="全球疫情信息 截至" + data['截止日期'],
pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(font_size=21, color='#51c2d5'),
pos_top='23%'),
yaxis_opts=opts.AxisOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False))
)
tu_5.add_js_funcs(
"""
var img = new Image(); img.src = './images/bg_title.png';
"""
)
page = Page(layout=Page.DraggablePageLayout, page_title="可视化大屏")
page.add(tu_1,tu_2,tu_3,tu_4,c_1,c_2,c_3,c_4,c_5,c_7,c_8,c_9,c_10,tu_5,c_11,c_12)
page.render('国内疫情信息.html')
def Style():
# 固定样式
Page.save_resize_html(source="./国内疫情信息.html", # 上面的HTML文件名称
cfg_file="chart_config_3.json", # 保存的json配置文件
dest="new_国内疫情信息可视化大屏.html" # 新HTML文件名称,可以空缺,默认resize_render.html
)
def main():
a=0
while True:
data_1 = China_data(Get_HTML())
data_2 = Abroad_data(Get_HTML())
data_3 = TimeLine()
data_4=Henan_data()
date=China_data_Total()['截止日期']
if date!=a:
View(data_1, data_2, data_3,data_4)
Style()
a=date
main()
import pandas as pd
df = pd.read_csv('国内疫情数据.csv')
# csv转excel
df.to_excel('C:\\Users\\fch\\Desktop\\excel1.xlsx',encoding='gbk')
# csv转txt
df.to_csv('C:\\Users\\fch\\Desktop\\txt.txt',encoding='gbk')
# txt转excel
with open('C:\\Users\\fch\\Desktop\\excel2.xlsx','w',encoding='gbk') as f:
data = open('C:\\Users\\fch\\Desktop\\txt.txt',encoding='gbk')
for line in data:
f.write(line)
import math
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
# 1.读取catering_sale.xls 餐饮数据,用describe对数据输出基本描述。
# data=pd.read_excel('D:\作业\韩毅数据分析\demo\data\catering_sale.xls')
# print(data)
# data=data.describe()
# print(data)
# 2.随机生成数据样本D,一行为3到10,一行为13~20,分别用Spearman、Pearon方法、计算输出其相关系数
# data=np.array([np.arange(3,11),np.arange(13,21)])
# print(data)
# data_1=stats.spearmanr(data[0],data[1])
# print("spearmanr方法:"+'\n',data_1)
# data_2=stats.pearsonr(data[0],data[1])
# print("Pearonr方法:"+'\n',data_2)
# 3.随机生成5*5的矩阵,计算输出该矩阵的均值、中位数、标准差、方差、协方差矩阵
# data=np.random.randint(0,200,size=(5,5),dtype=int)
# print(data)
# ave=np.mean(data)
# print("均值:",ave)
# Var=np.var(data)
# print("方差:",Var)
# Std=np.std(data)
# print("标准差:",Std)
# med=np.median(data)
# print("中位数:",med)
# cov=np.cov(data)
# print("协方差矩阵:",cov)
# 4.随机生成1000个正态分布的数据,画出该数据的箱式图
# data=np.random.rand(1000)
# plt.figure(figsize=(10,10))
# plt.boxplot(data)
# plt.show()
# -*- coding: utf-8 -*-
# 1、读入catering_sale.xls销量数据,找出异常值置为空值,采用拉格朗日插值法对空值进行插值,将结果输出至sales.xls
# import pandas as pd
# from scipy.interpolate import lagrange # 导入拉格朗日插值函数
# # 拉格朗日插值代码
# import pandas as pd #导入数据分析库Pandas
# import numpy as np
# import matplotlib.pyplot as plt
# from scipy.interpolate import lagrange #导入拉格朗日插值函数
# # inputfile = 'catering_sale.xls' #销量数据路径
# # outputfile = 'sales.xls' #输出数据路径
# data = pd.read_excel('catering_sale.xls') #读入数据
# temp = data[u'销量'][(data[u'销量'] < 400) | (data[u'销量'] > 5000)] #找到不符合要求得值 data[列][行]
# for i in range(temp.shape[0]):
# data.loc[temp.index[i],u'销量'] = np.nan #把不符合要求得值变为空值
# #自定义列向量插值函数
# #s为列向量,n为被插值的位置,k为取前后的数据个数,默认为5
# def ployinterp_column(s, n, k=5):
# y = s.iloc[list(range(n-k, n)) + list(range(n+1, n+1+k))] #取数 就是传入得data
# y = y[y.notnull()] #剔除空值
# f = lagrange(y.index, list(y))
# return f(n) #插值并返回插值结果
# #逐个元素判断是否需要插值
# for i in data.columns:
# for j in range(len(data)):
# if (data[i].isnull())[j]: #如果为空即插值。
# data.loc[j,i] = ployinterp_column(data[i], j)
# data.to_excel('sales.xls') #输出结果,写入文件
# print("success")
# 2、读入principal_component.xls数据,使用PCA降维,输出模型的各个特征向量及各个成分各自的方差百分比。
# -*- coding: utf-8 -*-
# 代码4-6 主成分分析降维
# import pandas as pd
# from sklearn.decomposition import PCA
# # 参数初始化
# data = pd.read_excel('principal_component.xls', header = None) # 读入数据
# pca = PCA()
# pca.fit(data)
# pca.components_ # 返回模型的各个特征向量
# pca.explained_variance_ratio_ # 返回各个成分各自的方差百分
# # 代码4-7 计算成分结果
# pca = PCA(3)
# pca.fit(data)
# low_d = pca.transform(data) # 用它来降低维度
# pd.DataFrame(low_d).to_excel('dimention_reducted.xls') # 保存结果
# pca.inverse_transform(low_d) # 必要时可以用inverse_transform()函数来复原数据
# low_d
# 对一个10×4维的随机生成矩阵进行主成分分析
# import pandas as pd
# import numpy as np
# D = pd.Series([1, 1, 2, 3, 5])
# D.unique()
# np.unique(D)
# from sklearn.decomposition import PCA
# D = np.random.rand(10,4)
# pca = PCA()
# pca.fit(D)
# pca.components_ # 返回模型的各个特征向量
# pca.explained_variance_ratio_ # 返回各个成分各自的方差百分比
#
# print(pca.explained_variance_ratio_ )
# 1.创建一个数值范围为0-1,间隔为0.1的数组。
# list=np.arange(0,1,0.1)
# print(list)
# 创建100个服从正态分布的随机数。
# data=np.random.rand(100)
# print(data)
# 3.对创建的两个四行三列的二维数组进行合并运算。
# data_1=np.random.randint(0,20,size=(4,3))
# data_2=np.random.randint(40,60,size=(4,3))
# # 垂直合并
# data1=np.vstack((data_1,data_2))
# #水平合并
# data2=np.hstack((data_1,data_2))
# print(data_1)
# print(data_2)
# print("垂直合并:"+'\n',data1)
# print("水平合并:"+'\n',data2)
num=input("请输入股票代码:")
name=input("请输入股票名称:")
M_max=input("请输入当天股票最高价:")
M_min=input("请输入当天股票最低价:")
print("股票代码:{},股票名称{}".format(num,name))
print("最高价:{},最低价:{},差值:{}".format(M_max,M_min,int(M_max)-int(M_min)))
# re=int(input("请输入实部:"))
# vi=int(input("请输入虚部:"))
# print("复数形式:{}+{}i,模:{}".format(re,vi,math.sqrt(re*re+vi*vi)))
# type=int(input("请输入客户类型:"))
# price=int(input("请输入价格:"))
# num=int(input("请输入订货量:"))
# if type<5:
# if num<800:
# money=num*price
# else:
# money=num*price*(1-0.02)
# else:
# if num<500:
# money = num * price * (1 - 0.03)
# elif num>=500 and num<1000:
# money = num * price * (1 - 0.05)
# elif num>=1000 and num<2000:
# money = num * price * (1 - 0.08)
# else:
# money = num * price * (1 - 0.1)
# print("money:",money)
# sum=0
# for i in range(100,201):
# if i%2!=0:
# sum+=i
# print(sum)
# x=int(input("请输入一个自然数:"))
# for i in range(x-1,1,-1):
# # print("sfds3")
# if x%i==0:
# print(i)
# break
# for i in range (2,101):
# for j in range(2,i):
# if i%j==0:
# break
# else:
# print(i,end=' ')