Python中Scrapy爬虫框架解析UTF-8编码后报错“没有xpath”如何解决?

response.body.decode(encoding="utf-8") linkList =response.body.decode(encoding="utf-8").xpath( '//td[[@class](/user/class)="pming_black12 ms-rteTableOddCol-BlueTable_CHI"]/a/[@href](/user/href)')

报错如下

ttributeError: 'str' object has no attribute 'xpath' 请问如何写才是正确的


Python中Scrapy爬虫框架解析UTF-8编码后报错“没有xpath”如何解决?

11 回复

这个问题通常是因为网页编码处理不当,导致解析后的HTML结构损坏,XPath无法定位元素。

首先确认你正确配置了编码处理:

class MySpider(scrapy.Spider):
    name = 'myspider'
    
    def start_requests(self):
        # 明确指定编码
        yield scrapy.Request(
            url='your_url',
            callback=self.parse,
            headers={'Accept-Encoding': 'gzip, deflate'},
            meta={'dont_retry': True}
        )
    
    def parse(self, response):
        # 强制使用UTF-8解码
        body = response.body.decode('utf-8', errors='ignore')
        # 或者直接使用response.text,但确保编码正确
        selector = scrapy.Selector(text=body)
        
        # 现在使用XPath
        items = selector.xpath('//div[@class="target"]')
        for item in items:
            # 处理数据
            pass

如果还是不行,检查网页实际编码:

def parse(self, response):
    # 打印编码信息
    print(f"Response encoding: {response.encoding}")
    print(f"Headers: {response.headers.get('Content-Type')}")
    
    # 尝试不同编码
    encodings = ['utf-8', 'gbk', 'gb2312', 'iso-8859-1']
    for enc in encodings:
        try:
            body = response.body.decode(enc)
            selector = scrapy.Selector(text=body)
            # 测试XPath
            test = selector.xpath('//title')
            if test:
                print(f"Success with encoding: {enc}")
                break
        except:
            continue

另外,确保XPath表达式正确,先用简单的//title测试。如果页面是JavaScript渲染的,考虑用Splash或Selenium。

检查编码,确保HTML结构完整。

先 xpath 再 encoding,而且 xpath 对象需要 extract

我知道啊,现在怎么写才是正确的

linkList =response.xpath(u’//td[class=“pming_black12 ms-rteTableOddCol-BlueTable_CHI”]/a/href’).extract()这样写就是返回空了,你在 encoding 也不对啊

xpath 写错了吧,而且 href 不需要解码

真可怕

response.xpath( ‘//td[@class=“pming_black12 ms-rteTableOddCol-BlueTable_CHI”]/a/@href’)

有什么问题???

楼主这个用法不太对。1.xpath 是 selector 的方法,而 response.body 的类型是 bytes ; 2.楼上所说的 response.xpath 是 TextResponse 类(scrapy 的默认 downloader 会根据 content-type 自动转换)的方法,如果你用 response.xpath 提示报错,说明这个 response 的 content-type 不是文本格式(可能是图片,应用之类的)

我都说了这里的 response 返回的是乱码,你这样直接 xpath 匹配的肯定是空?你没明白我的意思?

因为编码不对是 乱码,所以 xpath 匹配不到

回到顶部