Scrapy Shell

Scrapy Shell İle Basit İşlemler


Ekran resmi " http://quotes.toscrape.com/random " sitesinden alınmıştır. Chrome'dan öğeyi "incele" dediğimizde yan taraftaki developer sayfası açılacak. div'lerden ve onun altında spanlar'dan göreceğiniz üzere text, author, tag kısmını alacağız.




$ scrapy shell "website_adi" komutunu terminalde yazarak scrapy'nin shell ortamında çalışmaya başlayalım.

$ scrapy shell http://quotes.toscrape.com/random
komutunu terminale verdiğim zaman komutta belirttiğim web sayfasını indirir. Biz response objesi ile shell'de işlemlerimizi yapacağız.


>>>print(response.text)
yazarak bütün sayfanın içeriğini alıyoruz.
css seçici ile author, text, tag kısımlarına erişelim şimdi.

>>> response.css('small.author')
output:
[<Selector xpath=u"descendant-or-self::small[@class and contains(concat(' ', normalize-space(@class), ' '), ' author ')]" data=u'<small class="author" itemprop="author">'>]

>>> response.css('small.author').extract()
output:

[u'<small class="author" itemprop="author">C.S. Lewis</small>']


>>> response.css('small.author::text').extract()
output:
[u'Mark Twain']

#Yazar adini almak icin
>>> response.css('small.author::text')[0].extract()
output:
u'Mark Twain'

#Sayfadaki text'i almak icin
>>> response.css('span.text::text').extract_first()
u'\u201cNever tell the truth to people who are not worthy of it.\u201d'

#Sayfadaki text'e ait tag(etiket)i almak icin
>>> response.css('a.tag::text').extract_first()
u'truth'


Bugün Scrapy Shell'e kısa bir göz attık. Örneklerimizde Scrapy framework ile bir web sayfasında ihtiyacımız olan verilere kolayca nasıl erişebildiğimizi gördük.


Yorumlar