3 Scrapy 爬取 (1)

以網頁 http://quotes.toscrape.com/ 為例
命令:
scrapy shell 'http://quotes.toscrape.com/'

In [4]: response.xpath('//*[@class="quote"]')
Out[4]: 
[<Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>,
 <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>]

In [5]: quotes = response.xpath('//*[@class="quote"]')

In [6]: quote = quotes[0]

In [7]: quote
Out[7]: <Selector xpath='//*[@class="quote"]' data=u'<div class="quote" itemscope itemtype="h'>

In [8]: quote.extract()
Out[8]: u'<div class="quote" itemscope itemtype="http://schema.org/CreativeWork">\n        <span class="text" itemprop="text">\u201cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\u201d</span>\n        <span>by <small class="author" itemprop="author">Albert Einstein</small>\n        <a href="/author/Albert-Einstein">(about)</a>\n        </span>\n        <div class="tags">\n            Tags:\n            <meta class="keywords" itemprop="keywords" content="change,deep-thoughts,thinking,world"> \n            \n            <a class="tag" href="/tag/change/page/1/">change</a>\n            \n            <a class="tag" href="/tag/deep-thoughts/page/1/">deep-thoughts</a>\n            \n            <a class="tag" href="/tag/thinking/page/1/">thinking</a>\n            \n            <a class="tag" href="/tag/world/page/1/">world</a>\n            \n        </div>\n    </div>'

對單個quote的處理:

In [9]: quote.xpath('.//*[@class="text"]')
Out[9]: [<Selector xpath='.//*[@class="text"]' data=u'<span class="text" itemprop="text">\u201cThe '>]

In [10]: quote.xpath('.//*[@class="text"]/text()').extract()
Out[10]: [u'\u201cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\u201d']

In [11]: quote.xpath('.//*[@class="text"]/text()').extract_first()
Out[11]: u'\u201cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\u201d'

上面是用class藕溅,也可以用itemprop

text = quote.xpath('.//*[@itemprop="text"]/text()').extract_first()

In [13]: text
Out[13]: u'\u201cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\u201d'

對于 custom quote乘碑,如果不加最前面那個點 . 的話:

In [16]: quote.xpath('//*[@itemprop="text"]/text()').extract()
Out[16]: 
[u'\u201cThe world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.\u201d',
 u'\u201cIt is our choices, Harry, that show what we truly are, far more than our abilities.\u201d',
 u'\u201cThere are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.\u201d',
 u'\u201cThe person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.\u201d',
 u"\u201cImperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.\u201d",
 u'\u201cTry not to become a man of success. Rather become a man of value.\u201d',
 u'\u201cIt is better to be hated for what you are than to be loved for what you are not.\u201d',
 u"\u201cI have not failed. I've just found 10,000 ways that won't work.\u201d",
 u"\u201cA woman is like a tea bag; you never know how strong it is until it's in hot water.\u201d",
 u'\u201cA day without sunshine is like, you know, night.\u201d']

有點神奇,具體為什么?我不知道九火。以后知道了再回來補吧。
對于 meta 標簽的 content部分的獲取拆融,語法稍微不同

quote.xpath('.//*[@itemprop="keywords"]/@content').extract()
Out[20]: [u'change,deep-thoughts,thinking,world']
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末染突,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子姻僧,更是在濱河造成了極大的恐慌规丽,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件撇贺,死亡現場離奇詭異赌莺,居然都是意外死亡,警方通過查閱死者的電腦和手機松嘶,發(fā)現死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門艘狭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人翠订,你說我怎么就攤上這事巢音。” “怎么了尽超?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵官撼,是天一觀的道長。 經常有香客問我橙弱,道長歧寺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任棘脐,我火速辦了婚禮斜筐,結果婚禮上,老公的妹妹穿的比我還像新娘蛀缝。我一直安慰自己顷链,他們只是感情好,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布屈梁。 她就那樣靜靜地躺著嗤练,像睡著了一般榛了。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上煞抬,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天霜大,我揣著相機與錄音,去河邊找鬼革答。 笑死战坤,一個胖子當著我的面吹牛,可吹牛的內容都是我干的残拐。 我是一名探鬼主播途茫,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼溪食!你這毒婦竟也來了囊卜?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤错沃,失蹤者是張志新(化名)和其女友劉穎栅组,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體捎废,經...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡笑窜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年致燥,在試婚紗的時候發(fā)現自己被綠了登疗。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡嫌蚤,死狀恐怖辐益,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情脱吱,我是刑警寧澤智政,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站箱蝠,受9級特大地震影響续捂,放射性物質發(fā)生泄漏。R本人自食惡果不足惜宦搬,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一牙瓢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧间校,春花似錦矾克、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酒繁。三九已至,卻和暖如春控妻,著一層夾襖步出監(jiān)牢的瞬間州袒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工弓候, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留稳析,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓弓叛,卻偏偏與公主長得像彰居,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子撰筷,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內容