无法写入xpath表达式以进入图像链接

时间:2017-04-30 10:17:39

标签: xpath

一个空白的课程只是弄乱了我的表情。这是图像链接的html元素:

<div data-index="0" style="width: 300px; left: 0px; transition-duration: 0ms; transform: translate(0px, 0px);"><img alt="" class="" src="https://images.craigslist.org/00G0G_40m7DOu6DO3_300x300.jpg"></div>

以下是我尝试的内容:

//div/img/@src

2 个答案:

答案 0 :(得分:0)

// DIV / IMG [@class =&#39; ABC&#39;] / @ SRC

尝试为您的类命名并使用xpath表达式

答案 1 :(得分:0)

In [27]: text = """ <div data-index="0" style="width: 300px; left: 0px; transiti
...: on-duration: 0ms; transform: translate(0px, 0px);"><img alt="" class=""
...:  src="https://images.craigslist.org/00G0G_40m7DOu6DO3_300x300.jpg"></di
...: v>"""

In [28]: from scrapy.selector import Selector
In [29]: sel = Selector(text=text)
In [30]: sel.xpath('//img[@class=""]/@src').extract()
Out[31]: ['https://images.craigslist.org/00G0G_40m7DOu6DO3_300x300.jpg']