目录
表达式及示例:
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'