V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
chu8129
V2EX  ›  问与答

scrapy 中的 xpath 怎么使用 contains( text(),‘型号’)限制结果??一直获取不到正确结果

  •  
  •   chu8129 · 2015-06-25 11:02:49 +08:00 · 2150 次点击
    这是一个创建于 3248 天前的主题,其中的信息可能已经有所发展或是发生改变。

    scrapy获取URL为: http://product.dangdang.com/1259526222.html

    想获取型号获取不到,想问下大家怎么获取?
    1、希望brand返回的是只包括型号的div;
    2、希望通过xpath就可以获取;

    在scrapy shell中测试是可以的,但是到了编写的时候就拿不到了。。。。

    代码:

    coding:cp936

    from scrapy.spider import BaseSpider
    from scrapy.selector import Selector
    class dangdang(BaseSpider):
     name='dangdang'
     allowed_domains=[]
     start_urls=['http://product.dangdang.com/1259526222.html']
     def parse(self,response):
      sel=Selector(response)
      title=sel.xpath('//div[@class="head"]/h1/text()').extract()
      brand='\u54c1\u724c'.decode('unicode_escape')#型号
      brand=sel.xpath(('//div[@class="mall_goods_foursort_style_frame" and contains(text(),%s)]/a/text()')%(brand)).extract()

    2 条回复    2015-06-25 23:10:59 +08:00
    imn1
        1
    imn1  
       2015-06-25 11:19:32 +08:00
    应该是编码问题
    SoloCompany
        2
    SoloCompany  
       2015-06-25 23:10:59 +08:00
    你最后拼凑出来的那个 xpath 表达式,contains 方法的第二个参数,可以不需要加引号的吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5645 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:54 · PVG 15:54 · LAX 00:54 · JFK 03:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.