2015-01-16 6 views
8
class AljazeeraSpider(XMLFeedSpider): 
    name = "aljazeera" 
    allowed_domains = ["aljazeera.com"] 
    start_urls = [ 
     'http://www.aljazeera.com/', 
    ] 

    def parse(self, response): 
     hxs = HtmlXPathSelector(response) # The xPath selector 
     titles = hxs.select('//div[contains(@class,"SkyScrapperBoxes")]/div[contains(@class,"skyscLines")]') 
     if not titles: 
      MailNotify().send_mail("Aljazeera", "Scraper Report") 

     items = [] 
     for titles in titles: 
      item = NewsItem() 
      item['title'] = escape(''.join(titles.select('a/text()').extract())) 
      item['link'] = "http://www.aljazeera.com" + escape(''.join(titles.select('a/@href').extract())) 
      item['description'] = '' 
      item = Request(item['link'], meta={'item': item}, callback=self.parse_detail) 
      items.append(item) 

     return items 

    def parse_detail(self, response): 
     item = response.meta['item'] 
     sel = HtmlXPathSelector(response) 
     detail = sel.select('//td[@class = "DetailedSummary"]') 
     item['details'] = remove_html_tags(escape(''.join(detail.select('p').extract()))) 
     item['location'] = '' 
     published_date = sel.select('//span[@id = "ctl00_cphBody_lblDate"]') 
     item['published_date'] = escape(''.join(published_date.select('text()').extract())) 

     return item 

मैं वर्तमान में वेबसाइट को क्रॉल करने के लिए स्केपर पर काम कर रहा हूं। मुझे पाइथन में एकजुट होने के बारे में कुछ जानकारी है। लेकिन, यह जांचने के लिए कि मैं कौन सा लिंक काम कर रहा हूं, और item['location'], item['details'] मूल्य वापस कर रहे हैं या नहीं, मैं सबसे अचूक कैसे लिख सकता हूं? मैंने स्केपर अनुबंध सीखा है लेकिन कुछ भी समझ नहीं सकता है। तो, इस मामले में सबसे ज्यादा कैसे लिख सकते हैं?मैं पाइथन का उपयोग कर वेब स्केपर में यूनिट परीक्षण कैसे लिख सकता हूं?

उत्तर

12

यदि हम विशेष रूप से मकड़ियों (पाइपलाइनों या लोडर नहीं) का परीक्षण करने के बारे में बात कर रहे हैं, तो हमने जो किया है उसे स्थानीय HTML फ़ाइल से "नकली प्रतिक्रिया" प्रदान की जाती है। नमूना कोड:

import os 
from scrapy.http import Request, TextResponse 

def fake_response(file_name=None, url=None): 
    """Create a Scrapy fake HTTP response from a HTML file""" 
    if not url: 
     url = 'http://www.example.com' 

    request = Request(url=url) 
    if file_name: 
     if not file_name[0] == '/': 
      responses_dir = os.path.dirname(os.path.realpath(__file__)) 
      file_path = os.path.join(responses_dir, file_name) 
     else: 
      file_path = file_name 

     file_content = open(file_path, 'r').read() 
    else: 
     file_content = '' 

    response = TextResponse(url=url, request=request, body=file_content, 
          encoding='utf-8') 
    return response 

फिर, अपने TestCase कक्षा में, fake_response() फ़ंक्शन को कॉल करें और parse() कॉलबैक के जवाब फ़ीड:

from unittest.case import TestCase 

class MyTestCase(TestCase): 
    def setUp(self): 
     self.spider = MySpider() 

    def test_parse(self): 
     response = fake_response('input.html') 
     item = self.spider.parse(response) 
     self.assertEqual(item['title'], 'My Title') 
     # ... 

कि के अलावा, आप निश्चित रूप से Item Loaders का उपयोग शुरू कर देना चाहिए इनपुट और आउटपुट प्रोसेसर के साथ - यह एक बेहतर मॉड्यूलरिटी प्राप्त करने में मदद करेगा और इसलिए, अलगाव - स्पाइडर केवल आइटम उदाहरण उत्पन्न करेगा, डाटा तैयारी और संशोधन लोड के अंदर incapsulated किया जाएगा आर, जो आप अलग से परीक्षण करेंगे।

संबंधित मुद्दे

 संबंधित मुद्दे