我使用的是scrappy shell,下面是html. html***url:https://quotes.toscrape.com/page/1/***我想要的是每一个span中的文本,但是有些事情让我很困扰。首先,我写了这样一段:
result = response.xpath('//div[@class="quote"]')
result.xpath('//span[@class="text"]/text()')
比一些奇怪的事情来了,这是从壳的结果
In [16]: result.xpath('//span[@class="text"]/text()')
Out[16]:
[<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The world as we have created it is a...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is our choices, Harry, that show ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“There are only two ways to live your...'>,
<Selector xpath='//span[@class="text"]/text()' data='“The person, be it gentleman or lady,...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Imperfection is beauty, madness is g...'>,
<Selector xpath='//span[@class="text"]/text()' data='“Try not to become a man of success. ...'>,
<Selector xpath='//span[@class="text"]/text()' data='“It is better to be hated for what yo...'>,
<Selector xpath='//span[@class="text"]/text()' data="“I have not failed. I've just found 1...">,
<Selector xpath='//span[@class="text"]/text()' data='“A woman is like a tea bag; you never...'>,
<Selector xpath='//span[@class="text"]/text()' data='“A day without sunshine is like, you ...'>]
这让我怀疑xpath实际上是如何工作的。
我试过css选择器,它工作正常,但我只是想知道为什么xpath会给予我这样一个荒谬的结果。你能帮我解决这个问题吗?非常感谢!它可以帮助我了解更多关于xpath的知识。
1条答案
按热度按时间zaq34kh61#
//span
选择文档中的所有span元素。如果只需要上下文节点的子树中的元素,请使用.//span
。