有經驗的人都知道,解析網站需要嘗試,看看得到的數據是不是想要的,那么在scrapy中怎么嘗試呢?
調試工具-shell
主要用於編寫解析器
命令行進入shell
scrapy shell url
這個命令其實是個下載器
shell 界面
獲取數據
解析命令 response.css('title'),驗證獲取的數據正確與否
退出shell
exit即可
用法2
直接輸入 scrapy shell 命令,進入shell模式
用法3
實際上在shell中編寫“選擇器表達式”也是要借助瀏覽器的,但是直接用瀏覽器打開網頁和用程序訪問到的response可能不同,因為有動態頁面,所以需要用瀏覽器打開程序訪問到的response。
view(response) 命令就是用瀏覽器打開response;實際上是一個本地的html文件
此時我們注意到:shell 界面中有個 Userful shortcuts,包含了幾個命令,其中包含了view(response), 還有上例中的 fetch(req)
css 選擇器
3個函數:response.css('css表達式')、extract()、extract_first()
3個提取內容:標簽、標簽屬性、標簽內容
標簽提取
css 表達式為標簽
response.css('title') 獲取 title 標簽 ,其他標簽方法相同,如body 、div 、p、a等
生成一個 Selector 列表,如上圖,相當於一個選擇器
css 表達式為 id 或 class
response.css('#id') response.css('.class')
方法
response.css('title').extract() 提取標簽列表,如 ['<title>爬蟲實驗室 - SCRAPY中文網提供</title>']
response.css('title').extract()[0] or response.css('title').extract_first() 獲取第一個元素,如 '<title>爬蟲實驗室 - SCRAPY中文網提供</title>'
response.css('title::text').extract_first() 提取title標簽中的text,如 '爬蟲實驗室 - SCRAPY中文網提供'
標簽屬性的提取
css表達式為 標簽名::attr(屬性名)
a::attr(href)
img::attr(src)
多層標簽的屬性
response.css('.class a::attr(href)') response.css('.class #id a')
class and id 層標簽之間空格即可
標簽內容的提取
css 表達式為 ::text
response.css("div::text").extract() 標簽的text response.css(".center::text").extract() class的text response.css(".post-content *::text").extract() class中所有標簽的text
* 表示所有標簽
XPath 選擇器
XPth 簡介
xpath 使用路徑表達式在xml文檔中選取節點。
請看一個xml例子
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
節點之間的關系
父: book 是 title 的父
子: year 是 book 的子
同胞: book year 是同胞
先輩:title 的先輩是 book bookstore
后代:bookstore 的后代是 book title
路徑表達式
表達式 | 描述 |
---|---|
nodename | 選取此節點的所有子節點。 |
/ | 從根節點選取。 |
// | 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。 |
. | 選取當前節點。 |
.. | 選取當前節點的父節點。 |
@ | 選取屬性。 |
舉例
路徑表達式 | 結果 |
---|---|
bookstore | 選取 bookstore 元素的所有子節點。 |
/bookstore | 選取根元素 bookstore。 注釋:假如路徑起始於正斜杠( / ),則此路徑始終代表到某元素的絕對路徑! |
bookstore/book | 選取屬於 bookstore 的子元素的所有 book 元素。 |
//book | 選取所有 book 子元素,而不管它們在文檔中的位置。 |
bookstore//book | 選擇屬於 bookstore 元素的后代的所有 book 元素,而不管它們位於 bookstore 之下的什么位置。 |
//@lang | 選取名為 lang 的所有屬性。 |
XPath 選擇器
3個函數:response.xpath('表達式')、extract()、extract_first()
4個提取內容:標簽、標簽屬性、標簽內容、標簽內的所有文字
標簽
省略
標簽屬性
表達式為 @屬性名
//@href 所有 href //ol//@href ol標簽下所有 href //ol[@class="page-navigator"]//@href 具有某屬性的標簽ol下的所有href
標簽內容
表達式為 //text()
//ul[@class='tags-list']//a//text()
標簽內的所有文字
表達式為 string(標簽)
提取標簽及其后代的所有文字,並連在一起
response.xpath("string(//div[@class='post-content'])").extract()
實例
路徑表達式 | 結果 |
---|---|
/bookstore/book[1] | 選取屬於 bookstore 子元素的第一個 book 元素。 |
/bookstore/book[last()] | 選取屬於 bookstore 子元素的最后一個 book 元素。 |
/bookstore/book[last()-1] | 選取屬於 bookstore 子元素的倒數第二個 book 元素。 |
/bookstore/book[position()<3] | 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。 |
//title[@lang] | 選取所有擁有名為 lang 的屬性的 title 元素。 |
//title[@lang='eng'] | 選取所有 title 元素,且這些元素擁有值為 eng 的 lang 屬性。 |
/bookstore/book[price>35.00] | 選取 bookstore 元素的所有 book 元素,且其中的 price 元素的值須大於 35.00。 |
/bookstore/book[price>35.00]/title | 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大於 35.00。 |
通配符
通配符 | 描述 |
---|---|
* | 匹配任何元素節點。 |
@* | 匹配任何屬性節點。 |
node() | 匹配任何類型的節點。 |
實例
路徑表達式 | 結果 |
---|---|
/bookstore/* | 選取 bookstore 元素的所有子元素。 |
//* | 選取文檔中的所有元素。 |
//title[@*] | 選取所有帶有屬性的 title 元素。 |
也可以用 BeautifulSoup 進行解析。