scrapy 中转换 utf-8 后说没有 xpath - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Ewig
V2EX    Python

scrapy 中转换 utf-8 后说没有 xpath

  •  
  •   Ewig 2019-01-01 17:39:51 +08:00 2827 次点击
    这是一个创建于 2505 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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' 请问如何写才是正确的

    10 条回复    2019-01-02 17:39:00 +08:00
    j0hnj
        1
    j0hnj  
       2019-01-01 17:54:03 +08:00
    随手查了一下,`xpath` 是 response 对象的方法:

    https://doc.scrapy.org/en/latest/topics/request-response.html#response-subclasses
    chengxiao
        2
    chengxiao  
       2019-01-01 18:02:45 +08:00
    先 xpath 再 encoding,而且 xpath 对象需要 extract
    Ewig
        3
    Ewig  
    OP
       2019-01-01 18:21:31 +08:00
    @j0hnj 我知道啊,现在怎么写才是正确的
    Ewig
        4
    Ewig  
    OP
       2019-01-01 18:22:25 +08:00
    @chengxiao linkList =response.xpath(u'//td[@class="pming_black12 ms-rteTableOddCol-BlueTable_CHI"]/a/@href').extract()这样写就是返回空了,你在 encoding 也不对啊
    ioven
        5
    ioven  
       2019-01-01 19:43:03 +08:00
    @Ewig xpath 写错了吧,而且 href 不需要解码
    xiaoxinxiaobai
        6
    xiaoxinxiaobai  
       2019-01-01 19:57:53 +08:00 via Android   1
    真可怕
    15399905591
        7
    15399905591  
       2019-01-02 09:46:39 +08:00
    response.xpath( '//td[@class="pming_black12 ms-rteTableOddCol-BlueTable_CHI"]/a/@href')

    有什么问题???
    animal
        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 不是文本格式(可能是图片,应用之类的)
    Ewig
        9
    Ewig  
    OP
       2019-01-02 17:38:24 +08:00
    @15399905591 我都说了这里的 response 返回的是乱码,你这样直接 xpath 匹配的肯定是空?你没明白我的意思?
    Ewig
        10
    Ewig  
    OP
       2019-01-02 17:39:00 +08:00
    @animal 因为编码不对是 乱码,所以 xpath 匹配不到
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1216 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 23:38 PVG 07:38 LAX 15:38 JFK 18:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86