scrapy 框架 #cmd命令行
scrapy 命令行格式 #scrapy <command> [options][args]
文件建立目录 #C:\Users\User>cd C:\Users\User\Desktop\py-pro
scrapy基本步骤
1.创建工程和spider模板
2.编写spider,在spider目录下创建自己的spider.py
3.编写itempipeline
4.优化配置策略
-----------------------------------------------------------------------------------------------------------------------------
命令 说明 格式
startproject 创建一个新工程 scrapy startproject <name> [dir]
genspider 创建一个爬虫 scrpay gensipder[options]<name><domain>
settings 获取爬虫配置信息 scrapy settings [options]
crawl 运行一个爬虫 scrapy crawl <spider_name>
# 这里的spider_name为spider.py 中 name = "movies"
# 即scrapy crawl movies 运行
list 列出项目中所有爬虫 scrapy list
shell 启动url调试命令行 scrapy shell [url] # 这个命令非常有用,可以帮助我们获取网页上的文本信息,并且输出为 .json 或 .jl 文件形式。
-----------------------------------------------------------------------------------------------------------------------------
request类 #http请求
.url-----------------------------------------------------------------------#request对应请求的url地址
.method-----------------------------------------------------------------#对应请求方法,‘get’
.headers---------------------------------------------------------------#字典类型风格的请求头
.body------------------------------------------------------------------#请求内容的主体,字符串类型
.meta-----------------------------------------------------------------#用户添加拓展信息
.copy()----------------------------------------------------------------#复制该请求
-----------------------------------------------------------------------------------------------------------------------------
response类 http响应
.url------------------------------------------------------------------#response对应url地址
.status-----------------------------------------------------------#http状态吗,默认200
.headers--------------------------------------------------------#response对应的头部信息
.body----------------------------------------------------------#response对应的响应内容,字符串类型
.flags-------------------------------------------------------#一组标记
.request-----------------------------------------------------#产生response类型对应的request对象
.copy()---------------------------------------------------------#复制该响应
.css()---------------------------------------------------------#功能类似于requests,抓取某个标签的数据等
xpath
获取链接-->@href
ex1:response.xpath('//*[@id="new_vs_hot_tv"]/div[2]/div/div[4]/div/a/@href').get()
Out[13]: 'https://v.qq.com/x/cover/ha7r9z89i9d234y.html'
获取文本-->text()
ex2:response.xpath('//*[@id="new_vs_hot_tv"]/div[2]/div/div[4]/div/a/text()').get()
Out[14]: '倚天屠龙记[会员抢先看]'
css选择器
获取链接-->a::href
获取文本-->a::text