扫码一下
查看教程更方便
以下是 htmlresponse 的一些示例,我们将有 htmlresponse 对象,它是用选择器实例化的,如下所示
res = selector(html_response)
我们可以从 html 响应正文中选择 h2 元素,它返回 selectorlist 对象作为
res.xpath("//h2")
我们可以从 html 响应正文中选择 h2 元素,它将 unicode 字符串列表返回为
res.xpath("//h2").extract()
它返回 h2 元素。
res.xpath("//h2/text()").extract()
它返回定义在 h2 标签下的文本,不包括 h2 标签元素。
我们可以运行 p 标签并将类属性显示为
for ele in res.xpath("//p"):
print ele.xpath("@class").extract()
下面是一些关于 xmlresponse 的例子,我们将有 xmlresponse 对象,它是用选择器实例化的,如下所示
res = selector(xml_response)
我们可以从 xml 响应正文中选择描述元素,它返回 selectorlist 对象作为
res.xpath("//description")
我们可以通过将命名空间注册为
res.register_namespace("g", "http://base.google.com/ns/1.0")
res.xpath("//g:price").extract()
当我们创建 scrapy 项目时,我们可以使用 selector.remove_namespaces() 方法删除命名空间,并使用元素名称与 xpaths 一起适当地工作。
在项目中不总是调用名称空间删除过程有两个原因