美文网首页Python爬虫实战程序员我的Python自学之路
通过Python爬取当当网,学正则表达式

通过Python爬取当当网,学正则表达式

作者: 小志Codings | 来源:发表于2020-12-29 21:25 被阅读0次

    前言

    在上一篇文章中讲述了正则表达式的使用方法,既然讲了那不来点实战性的文章嘛?那肯定是不行的,所以这次我就是用正则表达式爬取当当网的TOP500的图书。

    准备工作

    工欲善其事,必先利其器。写代码也同样是如此,所以在开始之前请先安装好requestst、pandas库。如果没有安装,请先安装好。

    # 安装requests
    pip install requests
    
    # 安装pandas
    pip install pandas
    
    

    需求分析

    本次我们需要提取出当当网TOP500的图书名称、初版时间、价格和作者姓名。

    获取书籍名称

    首先打开开发者工具,使用选择器定位到书籍名称。

    image

    从上图可以看到,书籍名称在title属性值上。

    具体正则表达式如下所示:

    pattern_name = re.compile('li.*?<div class="name">.*?title="(.*?)".*?</a>', re.S)
    

    获取作者姓名

    image

    从上图可以看到作者姓名在a标签的文本中,具体正则表达式代码如下所示:

     pattern_author = re.compile('li.*?<div class="publisher_info">.*?<a .*?target="_blank">(.*?)</a>', re.S)
    

    获取出版日期

    image

    从上图可以看到,出版日期在span标签内。具体正则表达式如下所示:

     pattern_time = re.compile('li.*?<div class="publisher_info">.*?<span>(.*?)</span>', re.S)
    

    获取价格

    image

    和出本日期一样,都是在span标签内,但是要注意它们之间的区别。具体正则表达式如下所示:

    pattern_price = re.compile('li.*?<div class="price">.*?<span class="price_n">(.*?)</span>', re.S)
    

    翻页处理

    打开网页你会发现并不是所有的的图书都在同一个页面,TOP500共有25页,每页20本书。所以接下来需要分析翻页时URL的变化规律。

    # 第一页
    http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1
    # 第二页
    http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-2
    # 第二十五页
    http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-25
    

    从上面不难看出变化的内容是最后一个数字,并且每一次都按加1的操作。

    功能实现

    获取每一页的URL地址

        def get_url(self, page):
            url = f'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-{page}'
            return url
    

    可以通过循环的方式,将1-25传值给page

    获取每一页的网页信息

        def get_html(self, url):
            html = self.session.get(url).content.decode('gb2312', 'ignore')
            return html
    

    将信息保存至csv文件

        def get_info(self, html):
            pattern_name = re.compile('li.*?<div class="name">.*?title="(.*?)".*?</a>', re.S)
            pattern_author = re.compile('li.*?<div class="publisher_info">.*?<a .*?target="_blank">(.*?)</a>', re.S)
            pattern_time = re.compile('li.*?<div class="publisher_info">.*?<span>(.*?)</span>', re.S)
            pattern_price = re.compile('li.*?<div class="price">.*?<span class="price_n">(.*?)</span>', re.S)
            name = re.findall(pattern_name, html)
            author = re.findall(pattern_author, html)
            print('清洗前:',author)
            # author = [i for i in author if '出版社' not in i]
            if page == 3:
                author = [i for i in author if '出版社' not in i]
            if page == 14:
                author = [i for i in author if '机械工业出版社' not in i]
            print('清洗后:',author)
            print(len(author))
            time = re.findall(pattern_time, html)
            price = re.findall(pattern_price, html)
            price = [i.replace('&yen;', '¥') for i in price]
            df = pd.DataFrame({
                'book_name': name,
                'author': author,
                'edition_time': time,
                'price': price
            })
            print(list(df['author']))
            return df
    

    在这里我需要说两个坑,也就是上面代码的两个if语句

    这两个坑是这样的,当匹配到第三页和第第十四页的时候,分别有一本书会出现两个作者信息,一个是作者,一个是出版社信息。那这样就会出现21个作者,20本书21个作者显然是不合理的,所以我使用两个if语句将这两个多余内容清理掉。

    最后

    本次爬取当当网的内容分享到这里就结束了,你将正则表达式学会了吗?

    如果你看到这里,相信本文对你还是有些许帮助的,这也是我写文章的初衷。

    路漫漫其修远兮,吾将上下而求索。

    我是啃书君,一个专注于学习的人,你懂的越多,你不懂的越多。更多精彩内容,我们下期再见!

    相关文章

      网友评论

        本文标题:通过Python爬取当当网,学正则表达式

        本文链接:https://www.haomeiwen.com/subject/vtiqoktx.html