zoukankan      html  css  js  c++  java
  • Python 开发轻量级爬虫07

    Python 开发轻量级爬虫

    (imooc总结07--网页解析器BeautifulSoup)

    BeautifulSoup下载和安装
        使用pip install 安装:在命令行cmd之后输入,pip install BeautifulSoup4
    
    BeautifulSoup语法
        分为三个部分。
        首先根据下载好的html网页字符串,我们创建一个BeautifulSoup这个对象,创建这个对象的同时就将整个文档字符串下载成一个DOM树。
        然后根据这个dom树,我们就可以进行各种节点的搜索,这里有两个方法find_all/find。find_all方法会搜索出所有满足要求的节点,
        find方法只会搜索出第一个满足要求的节点。这两个方法的参数是一模一样的。
        得到一个节点以后,我们就可以访问节点的名称、属性、文字,相应的,在搜索节点的时候,我们也可以按照节点名称进行搜索,按照节
        点的属性进行搜索,或按照节点的文字进行搜素,这里将节点内容分为名称、属性、文字。
    
    我们举例说明。
    下面是网页上一个链接:
        <a href=’123.html’ class=’article_link’> python </a>
        针对这样的链接我们怎样搜索它呢?
        按照节点名称:a
        节点属性:href = ‘123.html’
        节点属性:class=‘article_link’
        节点内容:python
    使用这三种方式,可以进行搜索和访问
    对应代码:
        首先创建beautifulSoup对象,from bs4 import BeautifulSoup。
        根据下载好的HTML网页字符串创建BeautifulSoup对象。
        我们可以传入三个参数:
        Soup = {
            html_doc,               #html文档字符串
            ‘html.parser’,          #html解析器
            from_encoding=’utf-8’   #html文档的编码
        }
    
        如果网页的编码和代码的编码不一致的话,解析过程中会出现乱码。这里可以对编码进行指定。
        通过这种方式,我们就创建了bs对象,并且下载好了这个dom。
    
        其次,搜索节点(find_all , find)
            #方法:find_all(name, attrs, string) 节点名称、节点属性、节点文字
            #查找所有标签为a的节点
            Soup.find_all(‘a’)
            #查找所有标签为a,链接符合/view/123.htm形式的节点
            Soup.find_all(‘a’,href=’/view/123.htm’)
    
            BS非常强大的地方是,对名字、属性、文字都可以传入一个正则表达式,来匹配对应的内容
                Soup.find_all(‘a’,href=’re.compile(r’/view/d+.htm’))
    
            #查找所有标签为div,class为adc,文字为python的节点
            Soup.find_all(‘div’,class_=’adc’,string=’Python’)
            为了避免和python冲突,将class后加下划线即:class_,
    
        通过find_all, find两个方法就可以搜索dom中所有节点。
    
        最后,得到节点以后就可以访问节点的信息。
        比如:
        #得到节点:<a href = ‘1.html’>python</a> 
        我们就可以获取查找到的节点的标签名称。Node.name
        #获取查找到的节点的href属性
        Node[‘href’] 以字典的形式可以访问到所有的属性。
        #获取查找到的a节点的链接文字
        Node.get_text()方法来获取文字
    
        通过以上创建bs对象、搜索dom树、访问节点的内容,我们就可以实现对整个下载好的网页所有节点的解析和访问。
    
        编写代码,测试bs这个模块的各种方法?
        
            from bs4 import BeautifulSoup
            import re
            html_doc = """
                <html><head><title>The Dormouse's story</title></head>
                <body>
                <p class="title"><b>The Dormouse's story</b></p>
    
                <p class="story">Once upon a time there were three little sisters; and their names were
                <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
                <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
                <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
                and they lived at the bottom of a well.</p>
    
                <p class="story">...</p>
                """
            print('获取所有的链接')
            links = soup.find_all('a')
            for link in links:
                print(link.name,link['href'],link.get_text())
    
            print('获取lacie的链接')
            link_node = soup.find('a',href='http://example.com/lacie')
            print(link_node.name,link_node['href'],link_node.get_text())
    
            print('正则匹配')
            link_node = soup.find('a',href=re.compile(r"ill"))
            print (link_node.name,link_node['href'],link_node.get_text())
    
            print('获取P段文字')
            p_node = soup.find('p',class_='title')
            print(p_node.name,p_node.get_text())
  • 相关阅读:
    linux下聊天工具的安装
    Linux上OpenLDAP集群
    Linux下python基础调试
    曾仕强主讲:易经的奥秘(全文讲义)
    Linux单网卡多个IP(或者多个网卡多个IP)设置
    单播、广播、组播的区别和特点
    谷歌招聘 变态15题你会做几道?
    Gartner再评RSA为网络欺诈检测领导者 狼人:
    云安全 安全领域的最大热点之一 狼人:
    金山毒霸专业版高调上线 宣称杀毒速度增3倍 狼人:
  • 原文地址:https://www.cnblogs.com/billyzh/p/5841026.html
Copyright © 2011-2022 走看看