首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从选择器对象列表中提取单个元素?

如何从选择器对象列表中提取单个元素?
EN

Stack Overflow用户
提问于 2020-08-13 09:28:02
回答 1查看 42关注 0票数 0

我试图通过获取所有产品元素并迭代ecah元素,从网站打印产品名称。

代码语言:javascript
复制
    def parse(self, response):
    result = response.xpath("//div[@class = 's-main-slot s-result-list s-search-results sg-row']")

    for item in result:
        for product in item.xpath("//div[@data-component-type='s-search-result']"):
            print(product.xpath("//h2/a/span/text()").extract())

,但它不是个别的,它是一次全部打印。当打印个人的名字时,它会打印列表中的所有名字。下面是上面代码的输出。

代码语言:javascript
复制
   ['Pampers Premium Care Pants Diapers, Small, 70 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Large, 88 Count', 'Pampers Premium Care Pants Diapers M
onthly Box Pack, Medium, 108 Count', 'Pampers New Diaper Pants Super Value Box, Large (Pack of 168)', 'Pampers Diaper Pants, Large, 64 Count', 'Pampers Diaper Pants,
 Medium, 76 Count', 'Pampers Diaper Pants, XL, 56 Count', 'Pampers Active Baby Diapers, New Born, 72 Count', 'Huggies Wonder Pants, Small Size Diapers, 86 Count', 'M
amyPoko Pants Standard Diapers, Small (Pack of 46)', 'Pampers Diaper Pants, Large, 64 Count', 'Pampers Diaper Pants, Small, 86 Count', 'Pampers Diaper Pants Monthly
Box Packs, X-Large, 112 Count', 'Pampers Diaper Pants, XX-Large, 28 Count', 'Pampers New Diapers Pants, Medium, 50 Count', 'Pampers Diaper Pants Monthly Box Pack, Me
dium, 152 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Medium, 108 Count', 'Pampers Baby Dry Pants Diapers Monthly Mega Box, Large, 128 Count', 'Pam
pers Premium Care Pants Diapers, Small, 70 Count', 'Pampers Premium Care Pants Diapers, Medium, 54 Count', 'Pampers Premium Care Pants Diapers, XXL, 30 Count', 'Pamp
ers Premium Care Pants Diapers Monthly Box Pack, XL, 72 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Large, 88 Count', 'Pampers Premium Care Pants D
iapers, Large, 44 Count', 'Pampers Premium Care Pants Diapers, New Born, 50 Count', 'Supples Baby Pants Diapers, Medium, 72 Count', 'Pampers Premium Care Pants Diape
rs, XXL, 30 Count', 'Pampers Active Baby Diapers - 62 Pieces (Medium)', 'Pampers New Diapers Pants, Large , 42 Count', 'Pampers Diaper Pants, Large, 44 Count', 'Pamp
ers Diaper Pants, New Baby, 86 Count', 'Pampers Premium Care Pants Diapers, XL, 36 Count', 'Pampers New Diaper Pants, Small, 36 Count', 'Pampers Diaper Pants, Small,
 58 Count', 'Pampers New Diapers Pants, New Born, 66 Count', 'Pampers New Diapers Pants, XXL , 16 Count', 'Pampers New Diaper Pants Super Value Box, Medium (Pack of
200)', 'Pampers New Diapers Pants, Extra Large, 34 Count', 'Pampers New Diapers Pants, Small, 56 Count', 'Pampers Premium Care Pants Diapers, Medium, 38 Count', 'Pam
pers Premium Care Pants Diapers, New Born, 50 Count', 'Pampers Premium Care Pants Diapers, Medium, 16 Count', 'Pampers Premium Care Pants Diapers, Small, 46 Count',
'Pampers New Diaper Pants, XXXL, 7 Count', 'Pampers New Diaper Pants Super Value Box, Extra Large (Pack of 136)', 'Pampers New Diaper Pants Super Value Box, Large (P
ack of 168)', 'Pampers Premium Care Pants Diapers, Large, 30 Count', 'Pampers Diaper Pants Monthly Box Pack, Small, 172 Count', 'Pampers New Diaper Pants, Medium, 28
 Count', 'Pampers New Diaper Pants, New Born, 20 Count', 'Pampers Baby Dry Diaper Pants, Large (60 Count)', 'Pampers New Diaper Pants, Small, 20 Count', 'Pampers Pre
mium Care Pants Diapers, XL, 24 Count', 'Accupressure for Babies : Pamper your toddler with a gentle massage! (New Mom  Series)', 'Pampers Premium Care Pants Diapers
, Large, 17 Count', 'Pampers Premium Care Pants Diapers, New Born, 24 Count', 'Pampers Premium Care Pants Diapers, Small, 21 Count', 'Pampers New Diaper Pants Super
Value Box, Small (Pack of 224)', 'Pampers Splashers Disposable Swim Pants Diapers, Large, 11 Count', 'Pampers Active Baby Diapers, Medium, 90 Count', 'Pampers Active
 Baby Diapers, New Born, 72 Count', 'Pampers Premium Care Super Value box pack, XL, 108 Count', 'Huggies Wonder Pants, Large Size Diapers, 64 Count', 'Pampers Active
 Baby Diapers, Large, 50 Count']

..(同一名单现正印刷60次)..

代码语言:javascript
复制
 ['Pampers Premium Care Pants Diapers, Small, 70 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Large, 88 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Medium, 108 Count', 'Pampers New Diaper Pants Super Value Box, Large (Pack of 168)', 'Pampers Diaper Pants, Large, 64 Count', 'Pampers Diaper Pants,
 Medium, 76 Count', 'Pampers Diaper Pants, XL, 56 Count', 'Pampers Active Baby Diapers, New Born, 72 Count', 'Huggies Wonder Pants, Small Size Diapers, 86 Count', 'MamyPoko Pants Standard Diapers, Small (Pack of 46)', 'Pampers Diaper Pants, Large, 64 Count', 'Pampers Diaper Pants, Small, 86 Count', 'Pampers Diaper Pants Monthly
Box Packs, X-Large, 112 Count', 'Pampers Diaper Pants, XX-Large, 28 Count', 'Pampers New Diapers Pants, Medium, 50 Count', 'Pampers Diaper Pants Monthly Box Pack, Me
dium, 152 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Medium, 108 Count', 'Pampers Baby Dry Pants Diapers Monthly Mega Box, Large, 128 Count', 'Pam
pers Premium Care Pants Diapers, Small, 70 Count', 'Pampers Premium Care Pants Diapers, Medium, 54 Count', 'Pampers Premium Care Pants Diapers, XXL, 30 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, XL, 72 Count', 'Pampers Premium Care Pants Diapers Monthly Box Pack, Large, 88 Count', 'Pampers Premium Care Pants Diapers, Large, 44 Count', 'Pampers Premium Care Pants Diapers, New Born, 50 Count', 'Supples Baby Pants Diapers, Medium, 72 Count', 'Pampers Premium Care Pants Diapers, XXL, 30 Count', 'Pampers Active Baby Diapers - 62 Pieces (Medium)', 'Pampers New Diapers Pants, Large , 42 Count', 'Pampers Diaper Pants, Large, 44 Count', 'Pampers Diaper Pants, New Baby, 86 Count', 'Pampers Premium Care Pants Diapers, XL, 36 Count', 'Pampers New Diaper Pants, Small, 36 Count', 'Pampers Diaper Pants, Small,58 Count', 'Pampers New Diapers Pants, New Born, 66 Count', 'Pampers New Diapers Pants, XXL , 16 Count', 'Pampers New Diaper Pants Super Value Box, Medium (Pack of
200)', 'Pampers New Diapers Pants, Extra Large, 34 Count', 'Pampers New Diapers Pants, Small, 56 Count', 'Pampers Premium Care Pants Diapers, Medium, 38 Count', 'Pampers Premium Care Pants Diapers, New Born, 50 Count', 'Pampers Premium Care Pants Diapers, Medium, 16 Count', 'Pampers Premium Care Pants Diapers, Small, 46 Count',
'Pampers New Diaper Pants, XXXL, 7 Count', 'Pampers New Diaper Pants Super Value Box, Extra Large (Pack of 136)', 'Pampers New Diaper Pants Super Value Box, Large (P
ack of 168)', 'Pampers Premium Care Pants Diapers, Large, 30 Count', 'Pampers Diaper Pants Monthly Box Pack, Small, 172 Count', 'Pampers New Diaper Pants, Medium, 28
 Count', 'Pampers New Diaper Pants, New Born, 20 Count', 'Pampers Baby Dry Diaper Pants, Large (60 Count)', 'Pampers New Diaper Pants, Small, 20 Count', 'Pampers Pre
mium Care Pants Diapers, XL, 24 Count', 'Accupressure for Babies : Pamper your toddler with a gentle massage! (New Mom  Series)', 'Pampers Premium Care Pants Diapers, Large, 17 Count', 'Pampers Premium Care Pants Diapers, New Born, 24 Count', 'Pampers Premium Care Pants Diapers, Small, 21 Count', 'Pampers New Diaper Pants SuperValue Box, Small (Pack of 224)', 'Pampers Splashers Disposable Swim Pants Diapers, Large, 11 Count', 'Pampers Active Baby Diapers, Medium, 90 Count', 'Pampers Active Baby Diapers, New Born, 72 Count', 'Pampers Premium Care Super Value box pack, XL, 108 Count', 'Huggies Wonder Pants, Large Size Diapers, 64 Count', 'Pampers Active Baby Diapers, Large, 50 Count']
EN

回答 1

Stack Overflow用户

发布于 2020-08-13 09:33:40

让您在xpath (.//h2/a/span/text())中添加.以获取单个值,并使用返回第一个值的get() methot,而不是使用重发值列表的extract()

代码语言:javascript
复制
 for item in result:
        for product in item.xpath("//div[@data-component-type='s-search-result']"):
            print(product.xpath(".//h2/a/span/text()").get())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63392092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档