標籤:follow tor dev 一個 utf-8 upd gif http ima
Scrapy終端是一個互動終端,供您在未啟動spider的情況下嘗試及調試您的爬取代碼。 其本意是用來測試提取資料的代碼,不過您可以將其作為正常的Python終端,在上面測試任何的Python代碼。
該終端是用來測試XPath或CSS運算式,查看他們的工作方式及從爬取的網頁中提取的資料。 在編寫您的spider時,該終端提供了互動性測試您的運算式代碼的功能,免去了每次修改後運行spider的麻煩。
一旦熟悉了Scrapy終端後,您會發現其在開發和調試spider時發揮的巨大作用。
如果您安裝了 IPython ,Scrapy終端將使用 IPython (替代標準Python終端)。 IPython 終端與其他相比更為強大,提供智能的自動補全,高亮輸出,及其他特性。
我們強烈推薦您安裝 IPython ,特別是如果您使用Unix系統(IPython 在Unix下工作的很好)。 詳情請參考 IPython installation guide 。
啟動終端
您可以使用 shell 來啟動Scrapy終端:
<url> 是您要爬取的網頁的地址。
scrapy shell <url>
列印日誌:
scrapy shell ‘http://scrapy.org‘
不列印日誌:
scrapy shell ‘http://scrapy.org‘ --nolog
使用終端
D:\項目\小項目\scrapy_day6_httpbin\httpbin>scrapy shell "https://dig.chouti.com" --nologhttps://www.zhihu.com/captcha.gif?r=1512028381914&type=login[s] Available Scrapy objects:[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)[s] crawler <scrapy.crawler.Crawler object at 0x04E60090>[s] item {}[s] request <GET https://dig.chouti.com>[s] response <200 https://dig.chouti.com>[s] settings <scrapy.settings.Settings object at 0x04E60390>[s] spider <DefaultSpider ‘default‘ at 0x5a23f70>[s] Useful shortcuts:[s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)[s] fetch(req) Fetch a scrapy.Request and update local objects[s] shelp() Shell help (print this help)[s] view(response) View response in a browser
Scrapy終端僅僅是一個普通的Python終端(或 IPython )。其提供了一些額外的捷徑。
可用的快捷命令(shortcut)
shelp() - 列印可用對象及快捷命令的協助列表
fetch(request_or_url) - 根據給定的請求(request)或URL擷取一個新的response,並更新相關的對象
view(response) - 在原生瀏覽器開啟給定的response。 其會在response的body中添加一個 <base> tag ,使得外部連結(例片及css)能正確顯示。 注意,該操作會在本地建立一個臨時檔案,且該檔案不會被自動刪除。
可用的Scrapy對象
Scrapy終端根據下載的頁面會自動建立一些方便使用的對象,例如 Response 對象及 Selector 對象(對HTML及XML內容)。
這些對象有:
crawler - 當前 Crawler 對象.
spider - 處理URL的spider。 對當前URL沒有處理的Spider時則為一個 Spider 對象。
request - 最近擷取到的頁面的 Request 對象。 您可以使用 replace() 修改該request。或者 使用 fetch 捷徑來擷取新的request。
response - 包含最近擷取到的頁面的 Response 對象。
sel - 根據最近擷取到的response構建的 Selector 對象。
settings - 當前的 Scrapy settings
列印當前請求的狀態代碼:
>>> response
<200 https://dig.chouti.com>
>>> response.headers
{b‘Date‘: [b‘Thu, 30 Nov 2017 09:45:06 GMT‘], b‘Content-Type‘: [b‘text/html; charset=UTF-8‘], b‘Server‘: [b‘Tengine‘], b‘Content-Language‘: [b‘en‘], b‘X-Via‘: [b‘1.1 bd157:10 (Cdn Ca
che Server V2.0)‘]}
嘗試我們的xpath運算式抽取內容
>>> sel.xpath(‘//a[@class="show-content color-chag"]/text()‘).extract_first()‘\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tt\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t【迅雷嘉獎維護公司利益員工 每人獎10萬】11月30日訊,迅雷與迅雷大資料近日發生“內訌”,雙方多次發布公告互相指責。對此,迅雷發布內部郵件,嘉獎在關鍵時刻維護公司利益的5名員工,並給予每人10萬元的獎勵。\n\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t‘>>> sel.xpath(‘//a[@class="show-content color-chag"]/text()‘).extract_first().strip()‘【迅雷嘉獎維護公司利益員工 每人獎10萬】11月30日訊,迅雷與迅雷大資料近日發生“內訌”,雙方多次發布公告互相指責。對此,迅雷發布內部郵件,嘉獎在關鍵時刻維護公司利益的5名員工,並給予每人10萬元的獎勵。‘
這裡也可以用css抽取
>>> sel.css(‘.part1 a::text‘).extract_first().strip()‘Netflix買下《白夜追兇》海外發行權,將在全球190多個國家和地區播出‘
view就有意思了,它其實就是把下載的html儲存。
>>> view(response)
True
列印當前請求的url
>>> response.url
‘https://dig.chouti.com‘
python爬蟲scrapy之scrapy終端(Scrapy shell)