Python实战计划学习笔记-爬取商品信息

结果图###

result.png

代码###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup

star_alls = []
star_all = []
infos = []
num = 0
path = 'D:/Users/Jessy zhou/Plan-for-combating-master/1_week/1.2 web-element/1.2homework/webpage/index.html'


def getText(item):
    return '' if item == 0 else ''.join(item.get_text())


def getSrc(item):
    return '' if item == 0 else ''.join(item.get('src'))


with open(path, 'r') as web_data:
    soup = BeautifulSoup(web_data.read(), 'html.parser')

    imgs = soup.select('div.thumbnail > img')
    titles = soup.select('div.caption > h4 > a')
    descs = soup.select('div.caption > p')
    prices = soup.select('div.caption > h4.pull-right')
    reviews = soup.select('div.ratings > p.pull-right')
    # stars =soup.select('div.ratings > p:nth-of-type(2)')
    stars = soup.find_all('span', 'glyphicon')

    for star in stars:
        span = str(star)
        if 'empty' in span:
            span_star = span.replace('<span class="glyphicon glyphicon-star-empty"></span>', u'☆')
        else:
            span_star = span.replace('<span class="glyphicon glyphicon-star"></span>', u'★')
        star_alls.append(span_star)
    star_alls = star_alls[2:]
    limit = len(star_alls) / 5
    while num < limit:
        star_all.append(star_alls[num * 5:(num + 1) * 5])
        num += 1

    for img, title, desc, price, review, star_c in zip(imgs, titles, descs, prices, reviews, star_all):
        data = {
            'img': getSrc(img),
            'title': getText(title),
            'desc': getText(desc),
            'price': getText(price),
            'review': getText(review),
            'star': star_c
        }
        infos.append(data)

总结###

1.为了显示★,需要加u以显示
2.find_all的方法也不是非常的简单,而且replace的字符串过长,应该可以得到简化

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容