Home > Software engineering >  why does the xpath that I write results in so many repeated result
why does the xpath that I write results in so many repeated result

Time:12-29

I am using scrapy shell. Here is the html. html url:https://quotes.toscrape.com/page/1/ What I wanted was the text in every span,but something disturbed me a lot. First,I write this:

result = response.xpath('//div[@]')
result.xpath('//span[@]/text()')

than something strange came,here is the result from the shell

In [16]: result.xpath('//span[@]/text()')
Out[16]: 
[<Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>,
 <Selector xpath='//span[@]/text()' data='“The world as we have created it is a...'>,
 <Selector xpath='//span[@]/text()' data='“It is our choices, Harry, that show ...'>,
 <Selector xpath='//span[@]/text()' data='“There are only two ways to live your...'>,
 <Selector xpath='//span[@]/text()' data='“The person, be it gentleman or lady,...'>,
 <Selector xpath='//span[@]/text()' data='“Imperfection is beauty, madness is g...'>,
 <Selector xpath='//span[@]/text()' data='“Try not to become a man of success. ...'>,
 <Selector xpath='//span[@]/text()' data='“It is better to be hated for what yo...'>,
 <Selector xpath='//span[@]/text()' data="“I have not failed. I've just found 1...">,
 <Selector xpath='//span[@]/text()' data='“A woman is like a tea bag; you never...'>,
 <Selector xpath='//span[@]/text()' data='“A day without sunshine is like, you ...'>]

It makes me doubtful about how xpath actually work.

I have tried css selector,it worked,but I just wondered why xpath would give me such a ridiculous result.Could you help me figure out this problem?Many thanks!It could help me know more about xpath.

CodePudding user response:

//span selects all span elements in the document. If you only want those in the subtree of the context node, use .//span.

  • Related