1. import requests
    from bs4 import BeautifulSoup
  2.  
  3. blslib="html5lib"
    user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
    headers={"user-agent":user_agent}
    def demo1(url):
    r=requests.get(url,headers=headers)
    print(r.encoding)
    data =r.text.encode(r.encoding).decode('gbk')
    s =BeautifulSoup(data,blslib)
    row=s.select('#newAlexa > table > tbody > tr') //定位到id=#newAlexa下面的table > tbody > tr #定位之间要空格
  1. print(row)
  2.  
  3. if __name__ =="__main__":
    url="http://www.ip138.com/post/"
    demo1(url)
  1. 1 html = """
  2. 2 <html><head><title>The Dormouse's story</title></head>
  3. 3 <body>
  4. 4 <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
  5. 5 <p class="story">Once upon a time there were three little sisters; and their names were
  6. 6 <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
  7. 7 <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
  8. 8 <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
  9. 9 and they lived at the bottom of a well.</p>
  10. 10 <p class="story">...</p>
  11. 11 """

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list
(1)通过标签名查找

  1. print soup.select('title')
  2. #[<title>The Dormouse's story</title>]
  3.  
  4. print soup.select('a')
  5. #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
  6.  
  7. print soup.select('b')
  8. #[<b>The Dormouse's story</b>]

(2)通过类名查找

  1. print soup.select('.sister')
  2. #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

(3)通过 id 名查找

  1. print soup.select('#link1')
  2. #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

(4)组合查找

组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开

  1. print soup.select('p #link1')
  2. #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

直接子标签查找

  1. print soup.select("head > title")
  2. #[<title>The Dormouse's story</title>]

(5)属性查找

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

  1. print soup.select("head > title")
  2. #[<title>The Dormouse's story</title>]
  3.  
  4. print soup.select('a[href="http://example.com/elsie"]')
  5. #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格

  1. print soup.select('p a[href="http://example.com/elsie"]')
  2. #[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

BeautifulSoup 使用select方法详解(通过标签名,类名, id,组合,属性查找)的更多相关文章

  1. 转python爬虫:BeautifulSoup 使用select方法详解

    1 html = """ 2 <html><head><title>The Dormouse's story</title> ...

  2. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  3. C# Process.Start()方法详解(转)

    C# Process.Start()方法详解 System.Diagnostics.Process.Start(); 能做什么呢?它主要有以下几个功能: 1.打开某个链接网址(弹窗). 2.定位打开某 ...

  4. linux select函数详解

    linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...

  5. 利用C#实现AOP常见的几种方法详解

    利用C#实现AOP常见的几种方法详解 AOP面向切面编程(Aspect Oriented Programming) 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 下面这篇文章主要 ...

  6. MP实战系列(十二)之封装方法详解(续二)

    继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后. 此次要讲的是关于查询. 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事. 1.selectB ...

  7. 转载 JS组件Bootstrap Select2使用方法详解

    JS组件Bootstrap Select2使用方法详解 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2016-01-26我要评论 这篇文章主要为大家介绍了JS组件Bootstrap Sel ...

  8. 关于 redux-saga 中 take 使用方法详解

    本文介绍了关于redux-saga中take使用方法详解,分享给大家,具体如下: 带来一个自己研究好久的API使用方法. redux-saga中effect中take这个API使用方式,用的多的是ca ...

  9. oracle 重置序列从指定数字开始的方法详解

    原文 oracle 重置序列从指定数字开始的方法详解 重置oracle序列从指定数字开始 declare n ); v_startnum ):;--从多少开始 v_step ):;--步进 tsql ...

随机推荐

  1. MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  2. window 10 企业版激活

    一. 用管理员权限打开CMD.EXE 接着输入以下命令: slmgr /ipk NPPR9-FWDCX-D2C8J-H872K-2YT43 弹出窗口提示:“成功的安装了产品密钥”. 继续输入以下命令: ...

  3. android开发资源

    android仿微信 http://www.oschina.net/code/snippet_253900_33261

  4. 最烂编程语言得主:javascript

    C++在我脑中一直是一门缺乏设计和远见的语言,其设计者也是缺少主见的人(我承认我对c++有一定偏见),在我看来,C++从一开始就是堆叠语言特性,成为最流行的语言,,只是这个时代将它推到了最前列,我心中 ...

  5. [转] JDBC中的Statement和PreparedStatement的区别

    以Oracle为例吧 Statement为一条Sql语句生成执行计划,如果要执行两条sql语句select colume from table where colume=1;select colume ...

  6. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  7. mysql 8.0给数据库添加用户和赋权

    -- 使用mysql 数据库 USE mysql -- 为mysql创建用户:case_dev 密码为:pass123 CREATE USER case_dev IDENTIFIED BY 'pass ...

  8. html与表格(table)相关的属性

    <table> 标签定义 HTML 表格.简单的 HTML 表格由 table 元素以及一个或多个 tr.th 或 td 元素组成.tr 元素定义表格行,th 元素定义表头,td 元素定义 ...

  9. ElasticSearh更新nested字段(Array数组)。怎么根据查询条件(query)复制一个(index)到新的Index how to update by query a nested fields data for elasticsearch

    GET usernested/_search { "query": { "nested": { "path": "tags&quo ...

  10. Unity投影器细节整理

    抽了个空整理下投影器 一般投影器需要两张贴图,一张Cookie,一张FallOff. Unity提供Light和Multiple两种自带shader,和粒子类似. Cookie需要非alpha贴图,F ...