Home > Back-end >  trying to get text from html via xpath with scrapy "Mobilya"
trying to get text from html via xpath with scrapy "Mobilya"

Time:10-28

Below is the HTML, I am working on and I am trying to get "Gardroplar" text but it return me empty

start with <ol /"> <svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-home"></use> </svg></a> </li> <li> <svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow1"></use> </svg> <a href="/mobilya/c/109">Mobilya</a> </li> <li> <svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow1"></use> </svg> <span > <a href="/mobilya/gardiroplar/c/109011" data-toggle="dropdown" aria-expanded="false">Gardıroplar<svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow7"></use> </svg> </a> <ul > <li ><a href="/mobilya/gardiroplar/c/109011">Gardıroplar</a> </li> <li ><a href="/gardiroplar/kapakli-gardiroplar/c/109011002">Kapaklı Gardıroplar<svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow1"></use> </svg></a> </li> <li ><a href="/gardiroplar/surgulu-gardiroplar/c/109011003">Sürgülü Gardıroplar<svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow1"></use> </svg></a> </li> <li ><a href="/gardiroplar/bez-dolaplar/c/109011001">Bez Dolaplar<svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow1"></use> </svg></a> </li> </ul> </span> </li> <li> <svg > <use xlink:href="/_ui/responsive/theme-alpha/images/icons.svg#icon-arrow1"></use> </svg> <a href="/gardiroplar/kapakli-gardiroplar/c/109011002">Kapaklı Gardıroplar</a> </li> </ol>

My code: response.xpath('//ol[@]//li[svg[contains(@class,"icon-arrow2")]]/text()').getall()

CodePudding user response:

In case icon-arrow2 class is fixed value there you can use the following XPath:

"//li[./*[contains(@class,'icon-arrow2')]]//a"

The complete command is

response.xpath("//li[./*[contains(@class,'icon-arrow2')]]//a/text()").getall()

CodePudding user response:

If you are just trying to get the data. Prophets answer is the way to go. If you want to understand what you were doing wrong...

You're using a css selector in an xpath statement. Assuming your path is accurate you would need to use the full text of the class.

response.xpath('//ol[@]//li[svg[contains(@class,"icon-arrow2")]]/text()').getall()
  • Related