程序部分:
def bubble_sort(data):
#冒泡排序
for i in range(0,len(data)):#注意不是len(data)-1,因为(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:/课程源码及作业参考答案/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())), # 正则表达式,目标输出结果为review:[65]
'addre: '+addre.get('src'),
#'review: '+review.get_text(),
'title: '+title.get_text()
]
print(data0)
info0.append(data0)
#以下程序根据星星数、价格、评论数进行排序
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('根据星星数排列:')
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('根据价格排列:')
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('根据评论数排列:')
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])
运行结果:
根据星星数排列:
['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']
根据价格排列:
['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']
根据评论数排列:
['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']
总结:
1. 网上基本都是数字和非数字字符组合而成的信息,想要获取其中的数字,正则表达式是目前较好用的方法。
2. 想要弄清网页上星星的路径,需要把每一个星星的路径复制pycharm中,逐一对比才能发现通用的星星路径。
3.本次作业对于理解网页结构很有帮助。
3. python中对列表或字典中元素的定位多用[],matlab中多用(),刚上手时并不习惯。
4. 想要对程序冒泡排序部分进行简化,但还没成功。
5. findall与find_all的区别尚未搞清。
6. 目前对于价格的排序只能将价格从¥94.44转化为9444,尚未想到较好的方法在中间插入小数点。