python scrapy shell 网址,response.xpath值为什么为空

2025-04-02 18:09:34
推荐回答(1个)
回答1:

>>> response.xpath('html/body/title/text()')
[]

表示的路径如下

html ----> html ----> body ----> title ----> text

由于出现了两个html,所以搜索不到。
下面给出三种写法:
1 从子节点开始的

>>> response.xpath('body/title/text()')

2 从根节点开始的

>>> response.xpath('/html/body/title/text()')

3 从任意节点开始的

>>> response.xpath('//body/title/text()')
>>> response.xpath('//html/body/title/text()')