
response.body.decode(encoding="utf-8") linkList =response.body.decode(encoding="utf-8").xpath( '//td[@class="pming_black12 ms-rteTableOddCol-BlueTable_CHI"]/a/@href')
报错如下
ttributeError: 'str' object has no attribute 'xpath' 请问如何写才是正确的
1 j0hnj 2019-01-01 17:54:03 +08:00 随手查了一下,`xpath` 是 response 对象的方法: https://doc.scrapy.org/en/latest/topics/request-response.html#response-subclasses |
2 chengxiao 2019-01-01 18:02:45 +08:00 先 xpath 再 encoding,而且 xpath 对象需要 extract |
4 Ewig OP @chengxiao linkList =response.xpath(u'//td[@class="pming_black12 ms-rteTableOddCol-BlueTable_CHI"]/a/@href').extract()这样写就是返回空了,你在 encoding 也不对啊 |
6 xiaoxinxiaobai 2019-01-01 19:57:53 +08:00 via Android 真可怕 |
7 15399905591 2019-01-02 09:46:39 +08:00 response.xpath( '//td[@class="pming_black12 ms-rteTableOddCol-BlueTable_CHI"]/a/@href') 有什么问题??? |
8 animal 2019-01-02 11:29:09 +08:00 via Android 楼主这个用法不太对。1.xpath 是 selector 的方法,而 response.body 的类型是 bytes ; 2.楼上所说的 response.xpath 是 TextResponse 类(scrapy 的默认 downloader 会根据 content-type 自动转换)的方法,如果你用 response.xpath 提示报错,说明这个 response 的 content-type 不是文本格式(可能是图片,应用之类的) |
9 Ewig OP @15399905591 我都说了这里的 response 返回的是乱码,你这样直接 xpath 匹配的肯定是空?你没明白我的意思? |