Top500的书籍

注:此方法用到了当当网站进行学习,不将数据用于其他用途,仅用于学习

**使用工具:**python中的re、json、requests

准备工作:

url变化规律:

表示第一页:

复制代码
http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-1

表示第二页的url:

我们可以得出http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1- +num 其中num表示具体的页数

re正则表达式需要筛选的信息特征

确定筛选信息的主体部分

<li>
    <div class="list_num red">1.</div>   
    <div class="pic"><a href="http://product.dangdang.com/27911444.html" target="_blank"><img src="http://img3m4.ddimg.cn/77/13/27911444-1_l_11.jpg" alt="高尔基成长三部曲(童年、在人间、我的大学)"  title="高尔基成长三部曲(童年、在人间、我的大学)"/></a></div>    
    <div class="name"><a href="http://product.dangdang.com/27911444.html" target="_blank" title="高尔基成长三部曲(童年、在人间、我的大学)">高尔基成长三部曲(童年、在人间、我的大学)</a></div>    
    <div class="star"><span class="level"><span style="width: 94.2%;"></span></span><a href="http://product.dangdang.com/27911444.html?point=comment_point" target="_blank">151799条评论</a><span class="tuijian">100%推荐</span></div>    
    <div class="publisher_info">[苏]<a href="http://search.dangdang.com/?key=高尔基" title="[苏]高尔基 著,桑卓 译,酷威文化 出品" target="_blank">高尔基</a> 著,<a href="http://search.dangdang.com/?key=桑卓" title="[苏]高尔基 著,桑卓 译,酷威文化 出品" target="_blank">桑卓</a> 译,<a href="http://search.dangdang.com/?key=酷威文化" title="[苏]高尔基 著,桑卓 译,酷威文化 出品" target="_blank">酷威文化</a> 出品</div>    
    <div class="publisher_info"><span>2019-08-01</span>&nbsp;<a href="http://search.dangdang.com/?key=四川文艺出版社" target="_blank">四川文艺出版社</a></div>    

            <div class="biaosheng">五星评分:<span>109028次</span></div>
                      
    
    <div class="price">        
        <p><span class="price_n">&yen;54.00</span>
                        <span class="price_r">&yen;108.00</span>(<span class="price_s">5.0折</span>)
                    </p>
                    <p class="price_e">电子书:<span class="price_n">&yen;9.99</span></p>
                <div class="buy_button">
                          <a ddname="加入购物车" name="" href="javascript:AddToShoppingCart('27911444');" class="listbtn_buy">加入购物车</a>
                        
                        <a name="" href="http://product.dangdang.com/1901178320.html" class="listbtn_buydz" target="_blank">购买电子书</a>
                        <a ddname="加入收藏" id="addto_favorlist_27911444" name="" href="javascript:showMsgBox('addto_favorlist_27911444',encodeURIComponent('27911444&platform=3'), 'http://myhome.dangdang.com/addFavoritepop');" class="listbtn_collect">收藏</a>
     
        </div>

    </div>
  
    </li>  
查看hear部分,模拟浏览器:

其中,user-agent常用于模拟浏览器

代码:request:

def request_dandan(url):
    try:
        res=requests.get(url)
        if res.status_code  ==  200:
            return res.text
    except requests.RequestException:
        return None

代码:re部分:

def re_dandan(html):
    pattern=re.compile('<li>.*?list_num.*?(\d+).</div>.*?<img src="(.*?)".*?class="name".*?title="(.*?)">.*?class="star">.*?class="tuijian">(.*?)</span>.*?class="publisher_info">.*?target="_blank">(.*?)</a>.*?class="biaosheng">.*?<span>(.*?)</span></div>.*?<p><span\sclass="price_n">&yen;(.*?)</span>.*?</li>',re.S)
    items=re.findall(pattern,html)
    for item in items:
        yield{
            'range': item[0],
            'iamge':item[1],
            'title':item[2],
            'recommend':item[3],
            'author':item[4],
            'time':item[5],
            'price':item[6]
        }

代码:存储信息部分:

def write_item_to_file(item):
    with open('book.txt','a',encoding='UTF-8') as f:
        f.write(json.dumps(item, ensure_ascii=False)+'\n')
        f.close()

将这些代码拼装:

import json
import re
import requests

#requests部分
def request_dandan(url):
    try:
        res=requests.get(url)
        if res.status_code  ==  200:
            return res.text
    except requests.RequestException:
        return None



def re_dandan(html):
    pattern=re.compile('<li>.*?list_num.*?(\d+).</div>.*?<img src="(.*?)".*?class="name".*?title="(.*?)">.*?class="star">.*?class="tuijian">(.*?)</span>.*?class="publisher_info">.*?target="_blank">(.*?)</a>.*?class="biaosheng">.*?<span>(.*?)</span></div>.*?<p><span\sclass="price_n">&yen;(.*?)</span>.*?</li>',re.S)
    items=re.findall(pattern,html)
    for item in items:
        yield{
            'range': item[0],
            'iamge':item[1],
            'title':item[2],
            'recommend':item[3],
            'author':item[4],
            'time':item[5],
            'price':item[6]
        }
def write_item_to_file(item):
    with open('book.txt','a',encoding='UTF-8') as f:
        f.write(json.dumps(item, ensure_ascii=False)+'\n')
        f.close()

def main(i):
    url="http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-"+str(i)
    html=request_dandan(url)
    items=re_dandan(html)
    for item in items:
        write_item_to_file(item)

if __name__ == "__main__":
   for i in range(1,26):
       main(i)

最后,

正则表达式部分可能会出问题,正则表达式中pattern里的一个()表示一个被选择的部分,且在书写时我们常能看到.*? ,其中?表示非贪婪模式,本质上就是匹配的数据适可而止的意思。

相关推荐
bugtraq202112 小时前
闲鱼网页版开放,爬虫的难度指数级降低。
爬虫
Bigcrab__16 小时前
Python3网络爬虫开发实战(15)Scrapy 框架的使用(第一版)
爬虫·python·scrapy
九月镇灵将16 小时前
爬虫逆向学习(六):补环境过某数四代
爬虫·补环境·瑞数
kngines18 小时前
【PLW004】基于Python网络爬虫与推荐算法的新闻推荐平台v1.0(Python+Django+NLP+Vue+MySQL前后端分离)
爬虫·python·nlp·推荐算法
walk walk1 天前
免费爬虫软件“HyperlinkCollector超链采集器v0.1”
爬虫
亿牛云爬虫专家1 天前
如何通过subprocess在数据采集中执行外部命令 —以微博为例
爬虫·python·数据采集·多线程·代理ip·subprocess·微博
菜鸡中的奋斗鸡→挣扎鸡1 天前
初始爬虫5
爬虫
无敌开心1 天前
Django-Celery-Flower实现异步和定时爬虫及其监控邮件告警
爬虫·python·django