Python學習筆記--Python 爬蟲入門 -18-2 Scrapy-shell
阿新 • • 發佈:2018-12-10
# scrapy-shell
- scrapy shell教程 - shell - 啟動 - Linux: ctr+T,開啟終端,然後輸入scrapy shell "url:xxxx" - windows: scrapy shell "url:xxx" - 啟動後自動下載指定url的網頁
- 下載完成後,url的內容儲存在response的變數中,如果需要,我們需要呼叫response - response
>>> response.xpath('//title') [<Selector xpath='//title' data='<title>百度一下,你就知道</title>'>] >>> response.xpath('//title').extract() ['<title>百度一下,你就知道</title>'] >>> response.xpath('//title').extract()[0] '<title>百度一下,你就知道</title>'
- 爬取到的內容儲存在response中給 - response.body是網頁的程式碼 - resposne.headers是返回的http的頭資訊 - response.xpath()允許使用xpath語法選擇內容 - response.css()允許使用css語法選區內容 - selector - 選擇器,允許使用者使用選擇器來選擇自己想要的內容 - response.selector.xpath: response.xpath是selector.xpath的快捷方式 - response.selector.css: response.css是他的快捷方式 - selector.extract:把節點的內容用unicode形式返回 - selector.re:允許使用者通過正則選區內容