zoukankan      html  css  js  c++  java
  • 用webdriver模仿浏览器 爬取豆瓣python书单

     用webdriver模仿浏览器 爬取豆瓣python书单

    其中运用到os 模块 作用是生成文件夹 存储爬取的信息

    etree 用于xpath解析内容 详细代码如下 可用我的上一篇博客存取到excel当中

    import os
    import time
    from selenium import webdriver
    from lxml import etree
    #创建文件夹  没有指定的文件夹则创建 有则跳过
    root_dir = 'douban/img'
    if not os.path.exists(root_dir):
        os.mkdir(root_dir)
    
    #生成浏览器对象
    driver = webdriver.PhantomJS()
    
    #抓取页面函数
    def spider(page):
        base_url = 'https://book.douban.com/subject_search?search_text=python&cat=1001&start=%s'%(page*15)
        #用浏览器的get获取上述网址的网页
        driver.get(base_url)
        #阻塞四秒 让页面完全显示出来
        time.sleep(4)
        #命名文件名称和存储文件路径
        filename = root_dir + '/%s.png'%(page)
        #截屏并保存 这只是图片 在指定路径查找看是否有图片
        driver.save_screenshot(filename)
    
        #打印页面内容  可以打印出页面元素
        # print(driver.page_source)
    
        #在解析函数中解析页面元素
        content_parser(driver.page_source)
    
    #解析函数
    def content_parser(content):
        #我们这一次用xpath来解析  首先把上述内容生成树结构
        tree = etree.HTML(content)
        #在全部内容中 查找class为item-root的div 生成一个书列表
        books = tree.xpath('//div[@class="item-root"]')
    
        #遍历列表 获取每本书的信息
        for book in books:
            #拿取书皮图片信息
            book_src = book.xpath('./a/img/@src')
            if book_src != []:
                book_src = book_src[0]
    
            #书名
            book_name = book.xpath('.//div[@class="title"]/a')
            if book_name != []:
                book_name = book_name[0].text
    
            #书籍详细信息链接href
            book_href = book.xpath('.//div[@class="title"]/a/@href')
            if book_href != []:
                book_href = book_href[0]
    
            #评分
            book_rating = book.xpath('.//span[@class="rating_nums"]')
            if book_rating != []:
                book_rating = book_rating[0].text
    
            #细节 作者什么的
            book_detail = book.xpath('.//div[@class="meta abstract"]')
            if book_detail != []:
                book_detail = book_detail[0].text
    
            #打印需要一些时间 需要耐心等待哦
            print(book_src,book_name,book_href,book_rating,book_detail)
    
    
    
    #主进程
    if __name__ == '__main__':
        #打印10个页面信息 每个页面信息15本书
        for i in range(10):
            spider(i)
    '''
    book:
    <div class="item-root">
    
        <a href="https://book.douban.com/subject/26829016/" data-moreurl="onclick=&quot;
            moreurl(this,{i:'0',query:'python',subject_id:'26829016',from:'book_subject_search',
            cat_id:'1001'})&quot;" class="cover-link">
            <img src="https://img3.doubanio.com/lpic/s28891775.jpg" 
            alt="Python编程:从入门到实践 : 从入门到实践" class="cover" />
        </a>
        <div class="detail">
            
            <div class="title">
                <a href="https://book.douban.com/subject/26829016/" data-moreurl="onclick=&quot;
                moreurl(this,{i:'0',query:'python',subject_id:'26829016',from:'book_subject_search',
                cat_id:'1001'})&quot;" class="title-text">Python编程:从入门到实践 : 从入门到实践</a>
            </div>
            
            <div class="rating sc-bwzfXH hxNRHc">
                <span class="allstar45 rating-stars"></span>
                <span class="rating_nums">9.0</span>
                <span class="pl">(457人评价)</span>
            </div>
            
            <div class="meta abstract">
                [美]埃里克&middot;马瑟斯 / 袁国忠 / 人民邮电出版社 / 2016-7-1 / CNY 89.00
            </div>
            
            <div class="meta abstract_2"></div>
            
        </div>
    </div> 
    '''
  • 相关阅读:
    PostMan测试WebService接口
    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
    百度编辑器固定高度后图片框定位不准
    h5样式
    echarts-liquidfill 水球显示小数点
    工具
    linux使用windows磁盘,挂载共享目录
    微信订阅号关注问题
    linux 文件传输 SCP
    mysql 字符串截取
  • 原文地址:https://www.cnblogs.com/zhangboblogs/p/8561924.html
Copyright © 2011-2022 走看看