url = "https://xa.lianjia.com/chengjiao/pg{}/"
r = self.web_requests.get(url.format(1))
selector = Selector(text=r.text)
ul = selector.xpath("//ul[@class='listContent']/li")
for li in ul:
title = li.xpath('//div[@class="info"]/div[@class="title"]/a/text()').extract_first()
以上代码,循环取出的title一直是第一个li元素。解决方法就是xpath语法前加“.”,代表取当前元素。就这么简单的处理方式。。。
url = "https://xa.lianjia.com/chengjiao/pg{}/"
r = self.web_requests.get(url.format(1))
selector = Selector(text=r.text)
ul = selector.xpath("//ul[@class='listContent']/li")
for li in ul:
title = li.xpath('.//div[@class="info"]/div[@class="title"]/a/text()').extract_first()