截取数据
import requests
from bs4import BeautifulSoup
import xlwt
import xlrd
from pylabimport *
import matplotlib.pyplotas plt
if __name__ =='__main__':
rowNewNum =0
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('qunar数据')
data = ['景区名','地址','今日起售票数','经纬度','起始价格','城市名']
for jin range(0,6):
worksheet.write(0, j, data[j])
citys = ['北京' ,'上海' ,'成都','三亚','广州','重庆','深圳','西安','杭州','厦门','武汉','大连','苏州']
for cityObjcin citys:
for pagein range(1,5):
url ='https://piao.qunar.com/ticket/list.htm?keyword=' + cityObjc +'®ion=&from=mpl_search_suggest&page=' +str(page)
response = requests.get(url)
response.encoding ='utf-8'
text = response.text
bs_obj = BeautifulSoup(text,'html.parser')
arr = bs_obj.find('div', {'class':'result_list'}).contents
for iin arr:
rowNewNum = rowNewNum +1
info = i.attrs
name = info.get('data-sight-name')
address = info.get('data-address')
count = info.get('data-sale-count')
point = info.get('data-point')
price = i.find('span', {'class':'sight_item_price'})
price = price.find_all('em')
price = price[0].text
data = (name, address,int(count), point,float(price), cityObjc)
print("数据:", data)
存入excel
for colin range(0,6):
worksheet.write(rowNewNum, col, data[col])
# print('行数:',rowNum,'列数:',col,'数据:',data)
workbook.save('/Users/clairyang/PycharmProjects/untitled/test/qunaer.xlsx')
从excel读取写成📈图表
workfile = xlrd.open_workbook('/Users/clairyang/PycharmProjects/untitled/test/qunaer.xlsx')
sheet= workfile.sheets()[0]#横行titleArr
titleArr = sheet.col_values(0)
numArr = sheet.col_values(2)#售卖数量
priceArr = sheet.col_values(4)
plt.rcParams['font.sans-serif']='SimSun'
plt.title("去哪数据分析")
plt.plot(numArr,titleArr,'go-',label='销量',linewidth=2)
plt.plot(priceArr,titleArr,'rs',label='价格')
plt.show()
或者展示成柱图
plt.title("去哪数据分析")
plt.bar(titleArr,numArr,color='rgb')
plt.gcf().autofmt_xdate()
plt.xlabel('景点名')
plt.ylabel('销量')
plt.savefig('景点销量榜')
plt.show()