0

我想从以下 img 标签中提取网站http://www.snapdeal.com/product/aoc-e2060-swn-20-inch/622813?pos=0;85的品牌名称:

      <img src="http://i1.sdlcdn.com/img/brand/logo/2012-08-01-02-31-15-AOC.jpg" alt="Aoc" width="75" height="45">

即,我想提取“Aoc”。我试过这个:

   hxs.select('//*[@id="wrapper"]/div[2]/div[1]/div[3]/div[1]/ul/li[1]/img/@alt').extract()).strip()

但我得到的是空值。请帮忙。

4

2 回答 2

0

这是你想要的吗 ?

import re,requests
url=requests.get(" http://www.snapdeal.com/product/aoc-e2060-swn-20-inch/622813?pos=0;85")
re.findall(r'\<img src=.* alt="(.*)" width',url.text)
于 2013-10-15T13:00:39.540 回答
0
$ scrapy shell
<SNIP>
In [1]: fetch('http://www.snapdeal.com/product/aoc-e2060-swn-20-inch/622813?pos=0;85')
2013-10-16 00:37:08+0000 [default] INFO: Spider opened
2013-10-16 00:37:08+0000 [default] DEBUG: Crawled (200) <GET http://www.snapdeal.com/product/aoc-e2060-swn-20-inch/622813?pos=0;85> (referer: None)
<SNIP>
In [2]: hxs.select('//a[contains(@class, "brandName")]/img/@alt').extract()[0]
Out[2]: u'Aoc'

最好总是使用 XPath 尽可能“接近”您的目标。所有这些 div[1]/div[3]/span[1] 的废话都一样脆弱,并且很可能在页面更改时中断。

您使用"img/@alt".extract()获取 alt 属性没有任何问题。您到 img 节点的总体路径是错误的,仅此而已。

于 2013-10-16T00:39:55.173 回答