python实战作业1-2

代码:

from bs4 import BeautifulSoup


with open('F:/code/python/scap-mooc-netease/222/index.html', 'r') as web_data:#好像用相对路径不成功,这里的路径需要改下

    soup = BeautifulSoup(web_data, 'lxml')
    titles = soup.select(
        'body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a ')
    images = soup.select(
        'body > div > div > div.col-md-9 > div > div > div > img')
    reviews = soup.select(
        'body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right')
    prices = soup.select(
        'body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right')
    grades_texts = soup.select(
       'body > div > div > div.col-md-9 > div > div > div > div.ratings')#获取单个商品div中的所有内容
    grades=[]
    for grades_text in grades_texts:
        grades_craw=grades_text.find_all(class_="glyphicon glyphicon-star")#获取实心的星星
        grades_craw_empty=grades_text.find_all(class_="glyphicon glyphicon-star-empty")#获取空心的星星
        score=len(grades_craw)+0.5*len(grades_craw_empty)#实心的星星记为1分,空心的星星记为0.5分
        grades.append(score)


for title, image, review, price, grade in zip(titles, images, reviews, prices, grades):
    data = {
        'title': title.get_text(),
        'image': image.get('src'),
        'review': review.get_text(),
        'price': price.get_text(),
        'grade': grade
    }
    print(data)

输出结果

{'title': 'EarPod', 'grade': 5.0, 'review': '65 reviews', 'image': 'img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'price': '$24.99'}
{'title': 'New Pocket', 'grade': 4.5, 'review': '12 reviews', 'image': 'img/pic_0005_828148335519990171_c234285520ff.jpg', 'price': '$64.99'}
{'title': 'New sunglasses', 'grade': 4.5, 'review': '31 reviews', 'image': 'img/pic_0006_949802399717918904_339a16e02268.jpg', 'price': '$74.99'}
{'title': 'Art Cup', 'grade': 4.0, 'review': '6 reviews', 'image': 'img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'price': '$84.99'}
{'title': 'iphone gamepad', 'grade': 4.5, 'review': '18 reviews', 'image': 'img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'price': '$94.99'}
{'title': 'Best Bed', 'grade': 4.5, 'review': '18 reviews', 'image': 'img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'price': '$214.5'}
{'title': 'iWatch', 'grade': 4.5, 'review': '35 reviews', 'image': 'img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'price': '$500'}
{'title': 'Park tickets', 'grade': 4.5, 'review': '8 reviews', 'image': 'img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'price': '$15.5'}

find_all函数调试了比较久的时间,不过总体来说还是在控制范围内。

你可能感兴趣的:(python实战作业1-2)