css选择器,内置库 cssselect

目录

表达式及示例:

常用选择:

提取文本内容:

提取 url: 

提取

这种属性值有空格的情况

 



表达式及示例:

https://blog.csdn.net/qq_33472765/article/details/80843252

https://blog.csdn.net/qq_33472765/article/details/80740097

https://www.runoob.com/cssref/css-selectors.html

 

常用选择:

  • Select any tag: *
  • Select by tag <a>: a
  • Select by class of "link": .link
  • Select by tag <a> with class "link": a.link
  • Select by tag <a> with ID "home": a#home
  • Select by child <span> of tag <a>: a > span
  • Select by descendant <span> of tag <a>: a span
  • Select by tag <a> with attribute title of "Home": a[title=Home]

https://blog.csdn.net/zhoulizhu/article/details/79041471

 

提取文本内容:

<tr id="place">
    <td class="wp">
        zjk
    </td>
</tr>

解析后的 html 源码赋值给 html,一种获得 zjk 文本

td = html.cssselect('tr#place > td.wp')[0]
zjk = td.text_content()

选择 ID 为 place 的行元素,再是类为 wp 的子表格数据标签,cssselect 返回的是列表,我们需要获得第一个标签中的文本,调用 text_content 方法

.text

 

<p class="price_color">
    <font style="vertical-align: inherit;">
        <font style="vertical-align: inherit;">£51.77</font>
    </font>
</p>
p.price_color::text 获取文本

 

 

提取 url: 

<ul class="pager">
    <li class="current">
        <font style="vertical-align: inherit;">
            <font style="vertical-align: inherit;">
                第1页,共50页 
            </font>
        </font>
    </li>
    <li class="next">
        <a href="catalogue/page-2.html"> #!!!!!
            <font style="vertical-align: inherit;">
                <font style="vertical-align: inherit;">下一个</font>
            </font>
        </a>
    </li>       
</ul>
ul.pager li.next a::attr(href) 提取 url

 

 

提取 <div class="start myself"> 这种属性值有空格的情况

提取书名(h1),价钱(p),就是说可以只取空格前半部分或后半部分的属性值,或者用 . 代替空格

<div class="col-sm-6 product_main">         
        <h1>
            A Light in the Attic
        </h1>
        <p class="price_color">
            £51.77
        </p>
        <p class="instock availability">
            <i class="icon-ok"></i>
        In stock (22 available) 
        </p>
        <p class="star-rating Three">
            <i class="icon-star"></i>
            <i class="icon-star"></i>
            <i class="icon-star"></i>
            <i class="icon-star"></i>
            <i class="icon-star"></i>
        </p>
</div>    



>>> sel = response.css('div.product_main') 或者 .css(div.col-sm-6.product_main)
>>> sel.xpath('./h1/text()')
'A Light in the Attic'
>>> sel.css('p.price_color::text')
'£51.77'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值