程序部分:
def bubble_sort(data):
#冒泡排序
for i in range(0,len(data)):#注意不是len(data)-1,因?yàn)?a,b)表示從a到b-1
for j in range(0,len(data)):
if i>j:
if data[j]>data[i]:
tmp1=data[i]
data[i]=data[j]
data[j]=tmp1
return data
from bs4 import BeautifulSoup
path='D:/課程源碼及作業(yè)參考答案/Plan-for-combating-master/week1/1_2/1_2answer_of_homework/1_2_homework_required/index.html'
info0=[]
with open(path,'r') as wb_data:
soup=BeautifulSoup(wb_data,'lxml')
stars=soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)')
addres = soup.select('body > div > div > div.col-md-9 > div > div > div > img')
prices = soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right')
reviews = soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right')
titles = soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a')
#list(stars.stripped_strings)
import re
for star,addre, price, review, title in zip(stars,addres,prices,reviews,titles):
data0=[
'star: '+str(len(star.find_all('span',class_='glyphicon glyphicon-star'))),
'price: ' + price.get_text(),
'review: ' + str(re.findall(r'\d+', review.get_text())),? # 正則表達(dá)式,目標(biāo)輸出結(jié)果為review:[65]
'addre: '+addre.get('src'),
#'review: '+review.get_text(),
'title: '+title.get_text()
]
print(data0)
info0.append(data0)
#以下程序根據(jù)星星數(shù)宫蛆、價(jià)格艘包、評(píng)論數(shù)進(jìn)行排序
infos=[]
infop=[]
infor=[]
for k in range(0,len(info0)):
s=int(re.sub(r'\D+','',info0[k][0]))#將非字符串找到并用空格替換
p=float(re.sub(r'\D+','',info0[k][1]))#將非字符串找到并用空格替換
r=int(re.sub(r'\D+','',info0[k][2]))#將非字符串找到并用空格替換
infos.append(s)
infop.append(p)
infor.append(r)
print(bubble_sort(infos))
print(bubble_sort(infop))
print(bubble_sort(infor))
print('根據(jù)星星數(shù)排列:')
for i in range(0,len(info0)):
for j in range(0,len(info0)):
if int(re.sub(r'\D+', '', info0[i][0]))==int(infos[j]):
tmp=info0[i]
info0[i]=info0[j]
info0[j]=tmp
for i in range(0,len(info0)):
print(info0[i])
print('根據(jù)價(jià)格排列:')
for i in range(0,len(info0)):
for j in range(0,len(info0)):
if int(re.sub(r'\D+', '', info0[i][1]))==int(infop[j]):
tmp=info0[i]
info0[i]=info0[j]
info0[j]=tmp
for i in range(0,len(info0)):
print(info0[i])
print('根據(jù)評(píng)論數(shù)排列:')
for i in range(0,len(info0)):
for j in range(0,len(info0)):
if int(re.sub(r'\D+', '', info0[i][2]))==int(infor[j]):
tmp=info0[i]
info0[i]=info0[j]
info0[j]=tmp
for i in range(0,len(info0)):
print(info0[i])
運(yùn)行結(jié)果:
根據(jù)星星數(shù)排列:
['star: 4', 'price: $15.5', "review: ['8']", 'addre: img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'title: Park tickets']
['star: 4', 'price: $500', "review: ['35']", 'addre: img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'title: iWatch']
['star: 4', 'price: $214.5', "review: ['18']", 'addre: img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'title: Best Bed']
['star: 4', 'price: $94.99', "review: ['18']", 'addre: img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'title: iphone gamepad']
['star: 4', 'price: $74.99', "review: ['31']", 'addre: img/pic_0006_949802399717918904_339a16e02268.jpg', 'title: New sunglasses']
['star: 4', 'price: $64.99', "review: ['12']", 'addre: img/pic_0005_828148335519990171_c234285520ff.jpg', 'title: New Pocket']
['star: 3', 'price: $84.99', "review: ['6']", 'addre: img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'title: Art Cup']
['star: 5', 'price: $24.99', "review: ['65']", 'addre: img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'title: EarPod']
根據(jù)價(jià)格排列:
['star: 4', 'price: $15.5', "review: ['8']", 'addre: img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'title: Park tickets']
['star: 4', 'price: $500', "review: ['35']", 'addre: img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'title: iWatch']
['star: 4', 'price: $214.5', "review: ['18']", 'addre: img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'title: Best Bed']
['star: 5', 'price: $24.99', "review: ['65']", 'addre: img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'title: EarPod']
['star: 4', 'price: $64.99', "review: ['12']", 'addre: img/pic_0005_828148335519990171_c234285520ff.jpg', 'title: New Pocket']
['star: 4', 'price: $74.99', "review: ['31']", 'addre: img/pic_0006_949802399717918904_339a16e02268.jpg', 'title: New sunglasses']
['star: 3', 'price: $84.99', "review: ['6']", 'addre: img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'title: Art Cup']
['star: 4', 'price: $94.99', "review: ['18']", 'addre: img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'title: iphone gamepad']
根據(jù)評(píng)論數(shù)排列:
['star: 3', 'price: $84.99', "review: ['6']", 'addre: img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'title: Art Cup']
['star: 4', 'price: $15.5', "review: ['8']", 'addre: img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'title: Park tickets']
['star: 4', 'price: $94.99', "review: ['18']", 'addre: img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'title: iphone gamepad']
['star: 4', 'price: $214.5', "review: ['18']", 'addre: img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'title: Best Bed']
['star: 4', 'price: $64.99', "review: ['12']", 'addre: img/pic_0005_828148335519990171_c234285520ff.jpg', 'title: New Pocket']
['star: 4', 'price: $74.99', "review: ['31']", 'addre: img/pic_0006_949802399717918904_339a16e02268.jpg', 'title: New sunglasses']
['star: 4', 'price: $500', "review: ['35']", 'addre: img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'title: iWatch']
['star: 5', 'price: $24.99', "review: ['65']", 'addre: img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'title: EarPod']
總結(jié):
1. 網(wǎng)上基本都是數(shù)字和非數(shù)字字符組合而成的信息,想要獲取其中的數(shù)字耀盗,正則表達(dá)式是目前較好用的方法想虎。
2. 想要弄清網(wǎng)頁上星星的路徑,需要把每一個(gè)星星的路徑復(fù)制pycharm中叛拷,逐一對(duì)比才能發(fā)現(xiàn)通用的星星路徑舌厨。
3.本次作業(yè)對(duì)于理解網(wǎng)頁結(jié)構(gòu)很有幫助。
3. python中對(duì)列表或字典中元素的定位多用[]忿薇,matlab中多用()裙椭,剛上手時(shí)并不習(xí)慣。
4. 想要對(duì)程序冒泡排序部分進(jìn)行簡(jiǎn)化署浩,但還沒成功揉燃。
5. findall與find_all的區(qū)別尚未搞清。
6. 目前對(duì)于價(jià)格的排序只能將價(jià)格從¥94.44轉(zhuǎn)化為9444筋栋,尚未想到較好的方法在中間插入小數(shù)點(diǎn)炊汤。