zoukankan      html  css  js  c++  java
  • Python-爬虫-Beautifulsoup解析

    简介

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4

    复制代码
    #安装 Beautiful Soup
    pip install beautifulsoup4
    
    #安装解析器
    Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml .根据操作系统不同,可以选择下列方法来安装lxml:
    
    $ apt-get install Python-lxml
    
    $ easy_install lxml
    
    $ pip install lxml
    
    另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:
    
    $ apt-get install Python-html5lib
    
    $ easy_install html5lib
    
    $ pip install html5lib

    基本使用

    复制代码
    from bs4 import BeautifulSoup
    import requests,re
    req_obj = requests.get('https://www.baidu.com')
    soup = BeautifulSoup(req_obj.text,'lxml')

    '''标签查找'''
    print(soup.title) #只是查找出第一个
    print(soup.find('title')) #效果和上面一样
    print(soup.find_all('div')) #查出所有的div标签

    '''获取标签里的属性'''
    tag = soup.div
    print(tag['class']) #多属性的话,会返回一个列表
    print(tag['id']) #查找标签的id属性
    print(tag.attrs) #查找标签所有的属性,返回一个字典(属性名:属性值)

    '''标签包的字符串'''
    tag = soup.title
    print(tag.string) #获取标签里的字符串
    tag.string.replace_with("哈哈") #字符串不能直接编辑,可以替换

    '''子节点的操作'''
    tag = soup.head
    print(tag.title) #获取head标签后再获取它包含的子标签

    '''contents 和 .children'''
    tag = soup.body
    print(tag.contents) #将标签的子节点以列表返回
    print([child for child in tag.children]) #输出和上面一样


    '''descendants'''
    tag = soup.body
    [print(child_tag) for child_tag in tag.descendants] #获取所有子节点和子子节点

    '''strings和.stripped_strings'''
    tag = soup.body
    [print(str) for str in tag.strings] #输出所有所有文本内容
    [print(str) for str in tag.stripped_strings] #输出所有所有文本内容,去除空格或空行

    '''.parent和.parents'''
    tag = soup.title
    print(tag.parent)               #输出便签的父标签
    [print(parent) for parent in tag.parents] #输出所有的父标签

    '''.next_siblings 和 .previous_siblings
    查出所有的兄弟节点
    '''

    '''.next_element 和 .previous_element
    下一个兄弟节点
    '''

    '''find_all的keyword 参数'''
    soup.find_all(id='link2') #查找所有包含 id 属性的标签
    soup.find_all(href=re.compile("elsie")) #href 参数,Beautiful Soup会搜索每个标签的href属性:
    soup.find_all(id=True) #找出所有的有id属性的标签
    soup.find_all(href=re.compile("elsie"), id='link1') #也可以组合查找
    soup.find_all(attrs={"属性名": "属性值"}) #也可以通过字典的方式查找
    复制代码

    Practice

    from bs4 import BeautifulSoup as bs
    import urllib.request
    data=urllib.request.urlopen("http://edu.iqianyue.com/").read().decode("utf-8","ignore")
    bs1=bs(data)
    #格式化输出
    #print(bs1.prettify())
    #获取标签:bs对象.标签名
    bs1.title
    #获取标签里面的文字:bs对象.标签名.string
    bs1.title.string
    #获取标签名:bs对象.标签名.name
    bs1.title.name
    #获取属性列表:bs对象.标签名.attrs
    bs1.a.attrs
    #获取某个属性对应的值:bs对象.标签名[属性名] 或者 bs对象.标签名.get(属性名)
    bs1.a["class"]
    bs1.a.get("class")
    #提取所有某个节点的内容:bs对象.find_all('标签名') bs对象.find_all(['标签名1','标签名2,…,标签n'])
    bs1.find_all('a')
    bs1.find_all(['a','ul'])
    #提取所有子节点:bs对象.标签.contents bs对象.标签.children
    k1=bs1.ul.contents
    k2=bs1.ul.children
    allulc=[i for i in k2]
    #更多信息可以阅读官方文档:http://beautifulsoup.readthedocs.io/zh_CN/latest/
    

      

  • 相关阅读:
    可惜CodeSmith不直接支持Oracle数据库对象...
    Windows Vista
    关于Web Service ...
    他赚了多少钱?
    关于CodeSmith中模板的属性编辑...
    关于Session超时...
    奇怪的CheckBoxList...
    Devdays 2006 China | 微软开发者日
    第二阶段冲刺第一次会议
    冲刺阶段第一天
  • 原文地址:https://www.cnblogs.com/du-jun/p/10306061.html
Copyright © 2011-2022 走看看