文章目录
Scrapy——CSS选择器
CSS即层叠式表,其选择器是一种用来确定HTML文档中某部分位置的语言
CSS选择器的语法比XPath更简单一些,但功能不如XPath强大。实际上,当我们调用Selector对象的CSS方法时,在其内部会使用Python库cssselect将CSS选择器表达式,然后调用Select对象的XPath方法
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
body = """
<html>
<head>
<base href = "http://www.baidu.com"/>
<title>CSS Selector</title>
</head>
<body>
<div id = 'images-1' style = "width:1230px;">
<a href = 'image1.html'>Name:Image 1<br/><img src = 'image1.jpg'/></a>
<a href = 'image2.html'>Name:Image 2<br/><img src = 'image2.jpg'/></a>
<a href = 'image3.html'>Name:Image 3<br/><img src = 'image3.jpg'/></a>
</div>
<div id = 'images-2' class = 'small'>
<a href = 'image4.html'>Name:Image 4<br/><img src = 'image4.jpg'/></a>
<a href = 'image5.html'>Name:Image 5<br/><img src = 'image5.jpg'/></a>
</div>
</body>
</html>
"""
response = HtmlResponse(url="http://www.example.com", body&