我想从下面的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()但我得到的是空值。请帮帮忙。
发布于 2013-10-16 00:39:55
$ 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尽可能地“接近”目标。所有那些div3 1/div3 3/span1 1胡言乱语都是易碎的,当页面发生变化时,很可能会崩溃。
使用"img/@alt"和.extract()获取alt属性没有什么问题。您到img节点的总体路径是错误的,仅此而已。
发布于 2013-10-15 13:00:39
这就是你想要的吗?
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)https://stackoverflow.com/questions/19379809
复制相似问题