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

2015-06-25 11:10:42 +08:00
 chu8129
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()
4692 次点击
所在节点    Python
2 条回复
binbex
2015-06-25 12:16:46 +08:00
curl -o 1234.html http://product.dangdang.com/1259526222.html
然后在chrome中打开1234.html, 审查元素->Console,然后在控制台输入
$x('//*[contains(concat(" ", @class, " ")," pro_content ")]/textarea/text()'),你试试看这个结果是不是输出了"div class="mall_goods_foursort_style" ",那你也就明白了,你要的结果是在字符串内,也就是说
"div class="mall_goods_foursort_style" "你以为这个是xpath表达式路径,实际上这是个字符串,那你div[@class="mall_goods_foursort_style_frame"]肯定是没有结果的,你应该把$x('//*[contains(concat(" ", @class, " ")," pro_content ")]/textarea/text()')这个输出的字符串再转换为xml格式,这个用lxml的API可以做到,然后再用你上面自己的表达式应该可以了
chu8129
2015-11-16 16:36:52 +08:00
@binbex 非常感谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/201009

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX