Scrapy shell 是一個(gè)非常實(shí)用的爬蟲(chóng)測(cè)試工具,在爬蟲(chóng)的道路上調(diào)試是最優(yōu)的選擇,類(lèi)似于IPython,下面來(lái)講一下基本的用法
啟動(dòng)終端
scrapy shell <你想爬取的網(wǎng)址>
當(dāng)然你可以看一下幫助文檔 命令是scrapy shell -h
GET 請(qǐng)求
這個(gè)是非常常用的直接輸入url即可,示例:
scrapy shell https://www.baidu.com --nolog # --nolog 不打印日志
POST 請(qǐng)求
假如你想進(jìn)行post請(qǐng)求怎么辦呢局待?
那么我們使用只需和正常的python解釋器一樣操作即可,示例:
scrapy shell # 計(jì)入解釋器
formdata = {
"wd": "wkaanig" # json 格式
} # 你想提交的數(shù)據(jù)
req = scrapy.FormRequest('https://www.baidu.com',formdata=formdata) # 發(fā)送request 請(qǐng)求
# 如果想加入請(qǐng)求頭這樣 -> req = scrapy.FormRequest('https://www.baidu.com',formdata=formdata,headers='你的請(qǐng)求頭')
# 如果你沒(méi)有加 header 是用的scarpy自帶的 hearder
fetch(req) # 得到返回的response
response.text # 解析成文本
response.xpath('//p/text()').extract() # xpath 解析出你想要的數(shù)據(jù)
response.css('#container > div.content_none > div > p > span:nth-child(1)') # 你也可以用css 選擇器
# 這里只是說(shuō)明具體的網(wǎng)址用自己的規(guī)則
view(response) # 自動(dòng)打開(kāi)瀏覽器顯示得到的html,更直觀(guān)的截取想要的內(nèi)容