<img class="js-refreshCaptcha captcha" width="120" height="30" data-tip="s$t$看不清楚?换一张" alt="验证码" src="/captcha.gif?r=1462431202340&type=login" style="display: block;">
这个字段的src属性如何用xpath选择?
我的写法是:
captcha_url = response.xpath('/html/body/div[1]/div/div[2]/div[2]/form/div[1]/div[3]/div/img/@src').extract()
中间的xpath是从chrome直接copy的
但是无法显示出src属性,最后弹出的是[]
不知道哪里有问题、
另外如果知道css选择器怎么写的,也可以告知一下,我看到class中有空格,不知道如何选择.
我的写法是:
captcha_url = response.css('.js-refreshCaptcha .captcha::attr(src)').extract()
从Scrapy命令行中一步步测试得到,是不是因为是JS动态生成的原因?才会导致@src属性无法获取?
2020年了,我遇到了和楼主相似的问题。xpath返回img src为空,后来我尝试用python打印出整个html页面,发现打印出的html.txt中,img的URL实际上是写在了data-src属性中的,把scrapy中的@src改成@data-src后,就能返回正常的url了