zoukankan      html  css  js  c++  java
  • (转)python下很帅气的爬虫包

    官方文档地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

    Beautiful Soup 相比其他的html解析有个非常重要的优势。html会被拆解为对象处理。全篇转化为字典和数组。

    相比正则解析的爬虫,省略了学习正则的高成本。

    相比xpath爬虫的解析,同样节约学习时间成本。虽然xpath已经简单点了。(爬虫框架Scrapy就是使用xpath)

    安装

    linux下可以执行

    [plain] view plaincopy
     
    1. apt-get install python-bs4  


    也可以用python的安装包工具来安装

    [html] view plaincopy
     
    1. easy_install beautifulsoup4  
    2.   
    3. pip install beautifulsoup4  

    使用简介

    下面说一下BeautifulSoup 的使用。

    解析html需要提取数据。其实主要有几点

    1:获取指定tag的内容。

    [plain] view plaincopy
     
    1. <p>hello, watsy</p><br><p>hello, beautiful soup.</p>  

    2:获取指定tag下的属性。

    [html] view plaincopy
     
    1. <href="http://blog.csdn.net/watsy">watsy's blog</a>  

    3:如何获取,就需要用到查找方法。

    使用示例采用官方

    [html] view plaincopy
     
    1. html_doc = """  
    2. <html><head><title>The Dormouse's story</title></head>  
    3. <body>  
    4. <class="title"><b>The Dormouse's story</b></p>  
    5.   
    6. <class="story">Once upon a time there were three little sisters; and their names were  
    7. <href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,  
    8. <href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and  
    9. <href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;  
    10. and they lived at the bottom of a well.</p>  
    11.   
    12. <class="story">...</p>  
    13. """  

    格式化输出。

    [html] view plaincopy
     
    1. from bs4 import BeautifulSoup  
    2. soup = BeautifulSoup(html_doc)  
    3.   
    4. print(soup.prettify())  
    5. <html>  
    6. #  <head>  
    7. #   <title>  
    8. #    The Dormouse's story  
    9. #   </title>  
    10. #  </head>  
    11. #  <body>  
    12. #   <class="title">  
    13. #    <b>  
    14. #     The Dormouse's story  
    15. #    </b>  
    16. #   </p>  
    17. #   <class="story">  
    18. #    Once upon a time there were three little sisters; and their names were  
    19. #    <class="sister" href="http://example.com/elsie" id="link1">  
    20. #     Elsie  
    21. #    </a>  
    22. #    ,  
    23. #    <class="sister" href="http://example.com/lacie" id="link2">  
    24. #     Lacie  
    25. #    </a>  
    26. #    and  
    27. #    <class="sister" href="http://example.com/tillie" id="link2">  
    28. #     Tillie  
    29. #    </a>  
    30. #    ; and they lived at the bottom of a well.  
    31. #   </p>  
    32. #   <class="story">  
    33. #    ...  
    34. #   </p>  
    35. #  </body>  
    36. </html>  



    获取指定tag的内容

    [html] view plaincopy
     
    1. soup.title  
    2. <title>The Dormouse's story</title>  
    3.   
    4. soup.title.name  
    5. # u'title'  
    6.   
    7. soup.title.string  
    8. # u'The Dormouse's story'  
    9.   
    10. soup.title.parent.name  
    11. # u'head'  
    12.   
    13. soup.p  
    14. <class="title"><b>The Dormouse's story</b></p>  
    15.   
    16. soup.a  
    17. <class="sister" href="http://example.com/elsie" id="link1">Elsie</a>  


    上面示例给出了4个方面

    1:获取tag

    soup.title

    2:获取tag名称

    soup.title.name

    3:获取title tag的内容

    soup.title.string

    4:获取title的父节点tag的名称

    soup.title.parent.name

    怎么样,非常对象化的使用吧。

    提取tag属性

    下面要说一下如何提取href等属性。

    [html] view plaincopy
     
    1. soup.p['class']  
    2. # u'title'  


    获取属性。方法是

    soup.tag['属性名称']

    [html] view plaincopy
     
    1. <href="http://blog.csdn.net/watsy">watsy's blog</a>  

    常见的应该是如上的提取联接。

    代码是

    [html] view plaincopy
     
    1. soup.a['href']  

    相当easy吧。

    查找与判断

    接下来进入重要部分。全文搜索查找提取.

    soup提供find与find_all用来查找。其中find在内部是调用了find_all来实现的。因此只说下find_all

    [html] view plaincopy
     
    1. def find_all(self, name=None, attrs={}, recursive=True, text=None,  
    2.                  limit=None, **kwargs):  


    看参数。

    第一个是tag的名称,第二个是属性。第3个选择递归,text是判断内容。limit是提取数量限制。**kwargs 就是字典传递了。。

    举例使用。

    [html] view plaincopy
     
    1. tag名称  
    2. soup.find_all('b')  
    3. # [<b>The Dormouse's story</b>]  
    4.   
    5. 正则参数  
    6. import re  
    7. for tag in soup.find_all(re.compile("^b")):  
    8.     print(tag.name)  
    9. # body  
    10. # b  
    11.   
    12. for tag in soup.find_all(re.compile("t")):  
    13.     print(tag.name)  
    14. # html  
    15. # title  
    16.   
    17. 列表  
    18. soup.find_all(["a", "b"])  
    19. # [<b>The Dormouse's story</b>,  
    20. #  <class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,  
    21. #  <class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,  
    22. #  <class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]  
    23.   
    24. 函数调用  
    25. def has_class_but_no_id(tag):  
    26.     return tag.has_attr('class') and not tag.has_attr('id')  
    27.   
    28. soup.find_all(has_class_but_no_id)  
    29. # [<class="title"><b>The Dormouse's story</b></p>,  
    30. #  <class="story">Once upon a time there were...</p>,  
    31. #  <class="story">...</p>]  
    32.   
    33. tag的名称和属性查找  
    34. soup.find_all("p", "title")  
    35. # [<class="title"><b>The Dormouse's story</b></p>]  
    36.   
    37. tag过滤  
    38. soup.find_all("a")  
    39. # [<class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,  
    40. #  <class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,  
    41. #  <class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]  
    42.   
    43. tag属性过滤  
    44. soup.find_all(id="link2")  
    45. # [<class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]  
    46.   
    47. text正则过滤  
    48. import re  
    49. soup.find(text=re.compile("sisters"))  
    50. # u'Once upon a time there were three little sisters; and their names were '  

    获取内容和字符串

    获取tag的字符串
    [html] view plaincopy
     
    1. title_tag.string  
    2. # u'The Dormouse's story'  

    注意在实际使用中应该使用 unicode(title_tag.string)来转换为纯粹的string对象
     
    使用strings属性会返回soup的构造1个迭代器,迭代tag对象下面的所有文本内容
    [html] view plaincopy
     
    1. for string in soup.strings:  
    2.     print(repr(string))  
    3. # u"The Dormouse's story"  
    4. # u' '  
    5. # u"The Dormouse's story"  
    6. # u' '  
    7. # u'Once upon a time there were three little sisters; and their names were '  
    8. # u'Elsie'  
    9. # u', '  
    10. # u'Lacie'  
    11. # u' and '  
    12. # u'Tillie'  
    13. # u'; and they lived at the bottom of a well.'  
    14. # u' '  
    15. # u'...'  
    16. # u' '  


     
    获取内容
    .contents会以列表形式返回tag下的节点。
    [html] view plaincopy
     
    1. head_tag = soup.head  
    2. head_tag  
    3. <head><title>The Dormouse's story</title></head>  
    4.   
    5. head_tag.contents  
    6. [<title>The Dormouse's story</title>]  
    7.   
    8. title_tag = head_tag.contents[0]  
    9. title_tag  
    10. <title>The Dormouse's story</title>  
    11. title_tag.contents  
    12. # [u'The Dormouse's story']  
     
    想想,应该没有什么其他的了。。其他的也可以看文档学习使用。
     

    总结

    其实使用起主要是
    [html] view plaincopy
     
    1. soup = BeatifulSoup(data)  
    2. soup.title  
    3. soup.p.['title']  
    4. divs = soup.find_all('div', content='tpc_content')  
    5. divs[0].contents[0].string  
    转自   http://blog.csdn.net/watsy/article/details/14161201
  • 相关阅读:
    jdk1.8安装教程
    实现负载均衡的两种方式
    Java中ArrayList和LinkedList区别
    做一个英译中的命令行工具
    在 IDEA 中运行 Spark 程序报错:Multiple sources found for text.......please specify the fully qualified class name
    Android消息处理:EventBus、BroadCast和Handler-优缺点比较(转)
    C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter、转载)
    Android相关知识
    物联网相关知识
    Mqtt相关知识
  • 原文地址:https://www.cnblogs.com/wuxinqiu/p/3852901.html
Copyright © 2011-2022 走看看