Scrapy中extract_first()和extract()的区别

测试用到的爬取网站
在这里插入图片描述
在这里插入图片描述

In [11]: 
print(response.xpath('//h3/a/@title'))
# scrapy.selector.unified.SelectorList 是Selector组成的列表
Out[11]:
# 为了方便阅读换行符我手打的
[<Selector xpath='//h3/a/@title' data='A Light in the Attic'>,
<Selector xpath='//h3/a/@title' data='Tipping the Velvet'>, 
<Selector xpath='//h3/a/@title' data='Soumission'>, 
<Selector xpath='//h3/a/@title' data='Sharp Objects'>, 
<Selector xpath='//h3/a/@title' data='Sapiens: A Brief History of Humankind'>, 
<Selector xpath='//h3/a/@title' data='The Requiem Red'>,
<Selector xpath='//h3/a/@title' data='The Dirty Little Secrets of Getting Y...'>, 
<Selector xpath='//h3/a/@title' data='The Coming Woman: A Novel Based on th...'>,
<Selector xpath='//h3/a/@title' data='The Boys in the Boat: Nine Americans ...'>,]

In [9]: print(response.xpath('//h3/a/@title').extract())
# List
Out[9]:
# 为了方便阅读换行符我手打的
['A Light in the Attic', 
 'Tipping the Velvet', 
 'Soumission', 
 'Sharp Objects', 
 'Sapiens: A Brief History of Humankind', 
 'The Requiem Red', 
 'The Dirty Little Secrets of Getting Your Dream Job', 
 'The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull',
 'The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics', ]
In [7]: print(response.xpath('//h3/a/@title').extract_first())
# Str
Out[7]:
A Light in the Attic

在有很多数据情况下type()说明
直接Xpath是Selector组成的列表
Xpath.extract()List把爬到的数据组成一个列表
Xpath.extract_first()Str把爬到的数据组成一个列表提取第一个转化为Str格式
Xpath.get() Str把爬到的数据组成一个列表提取第一个转化为Str格式

别人博客的补充说明
https://www.ucloud.cn/yun/43396.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值