zoukankan      html  css  js  c++  java
  • python爬虫入门--beautifulsoup

    1,beautifulsoup的中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

    2,

    from bs4 import BeautifulSoup 
    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>
    """;
    soup = BeautifulSoup(html_doc);
    print(soup.prettify())

     1)soup.prettify()的作用是把html格式化输出

     2)在输出是会发出警告:No parser was explicitly specified, so I'm using the best available HTML parser for this system。这是因为没有解析器。所以需要安装解析器。如下图:

    3)soup = BeautifulSoup(html_doc,"html.parser");//这个就可以加入解析器

    print(soup.prettify())

    4)soup.title  #获取title内容<title>The Dormouse's story</title>

      soup.标签名  #获取对应的标签。(系统当前第一个)

    soup.find_all('a') #打印出所有‘a’标签 返回的是一个数组
    soup.find(id="link3") #打印出对应id页面

    for link in soup.find_all('a'): #这个用来遍历
    print(link.get('id'))

    #在遍历class时候返回的是一个数组
    print(link.get('class'))
    #['sister1']
    #['sister2']
    #['sister3']

    soup.get_text() #这个是用来获取所有的文字

    soup.find('p',{'class':'story'})) #这个里面是获取p标签下的class=story所有信息 注:这里因为class是关键字所以不能使用find('class':'story')
    soup.find('p',{'class':'story'}).string) # 结果为none
     

    5)可以通过政策表达式来 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到:

    import re
    for tag in soup.find_all(re.compile("^b")):
        print(tag.name)

     (5.1),python的正则表达式

    (注:图片来源https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html)



     
  • 相关阅读:
    Struts2学习笔记(四) Action(中)
    Struts2学习笔记(十) OGNL
    asp.net连接Access数据库。一般都怎么连有几种连法
    网络跃迁——C/S到B/S的“惊世一跃”
    用ASP打开远端MDB文件的方法
    solution to DreamweaverCtrls.dll
    for debugging on site,the web.config should be edited as follows
    For web.config setting,reference the book of
    1. need mssql connection method for dotnet vhost,etc
    蔡学镛推荐的编程语言REBOL编程初体验
  • 原文地址:https://www.cnblogs.com/lx-1024/p/8017513.html
Copyright © 2011-2022 走看看