2013-03-03 21 views
5

Tôi đang cố gắng lập trình một spider thu thập thông tin để thu thập dữ liệu các nguồn cấp dữ liệu RSS của một trang web và sau đó phân tích cú pháp các thẻ meta của bài viết.Cách sử dụng đúng Quy tắc, hạn chế_xpath để thu thập dữ liệu và phân tích cú pháp các URL có nhiều mẩu tin lưu niệm?

Trang RSS đầu tiên là trang hiển thị các danh mục RSS. Tôi đã quản lý để trích xuất liên kết vì thẻ nằm trong thẻ. Nó trông giống như thế này:

 <tr> 
      <td class="xmlLink"> 
      <a href="http://feeds.example.com/subject1">subject1</a> 
      </td> 
     </tr> 
     <tr> 
      <td class="xmlLink"> 
      <a href="http://feeds.example.com/subject2">subject2</a> 
      </td> 
     </tr> 

Khi bạn nhấp vào liên kết nó mang đến cho bạn các bài viết cho rằng thể loại RSS trông như thế này:

<li class="regularitem"> 
    <h4 class="itemtitle"> 
     <a href="http://example.com/article1">article1</a> 
    </h4> 
    </li> 
    <li class="regularitem"> 
    <h4 class="itemtitle"> 
     <a href="http://example.com/article2">article2</a> 
    </h4> 
    </li> 

Như Bạn có thể thấy tôi có thể nhận được liên kết với xpath một lần nữa nếu tôi sử dụng thẻ Tôi muốn trình thu thập thông tin của tôi truy cập vào liên kết bên trong thẻ đó và phân tích cú pháp thẻ meta cho tôi.

Đây là mã bánh xích của tôi:

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from tutorial.items import exampleItem 


class MetaCrawl(CrawlSpider): 
    name = 'metaspider' 
    start_urls = ['http://example.com/tools/rss'] # urls from which the spider will start crawling 
    rules = [Rule(SgmlLinkExtractor(restrict_xpaths=('//td[@class="xmlLink"]')), follow=True), 
     Rule(SgmlLinkExtractor(restrict_xpaths=('//h4[@class="itemtitle"]')), callback='parse_articles')] 

    def parse_articles(self, response): 
     hxs = HtmlXPathSelector(response) 
     meta = hxs.select('//meta') 
     items = [] 
     for m in meta: 
      item = exampleItem() 
      item['link'] = response.url 
      item['meta_name'] =m.select('@name').extract() 
      item['meta_value'] = m.select('@content').extract() 
      items.append(item) 
     return items 

Tuy nhiên đây là đầu ra khi tôi chạy bánh xích:

DEBUG: Crawled (200) <GET http://http://feeds.example.com/subject1> (referer: http://example.com/tools/rss) 
DEBUG: Crawled (200) <GET http://http://feeds.example.com/subject2> (referer: http://example.com/tools/rss) 

Tôi đang làm gì sai ở đây? Tôi đã đọc tài liệu lặp đi lặp lại nhưng tôi cảm thấy như tôi tiếp tục nhìn cái gì đó. Bất kỳ trợ giúp sẽ được đánh giá cao.

EDIT: Đã thêm: items.append (item). Đã quên nó trong bài gốc. EDIT:: Tôi đã cố gắng này là tốt và nó dẫn đến cùng một kết quả:

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from reuters.items import exampleItem 
from scrapy.http import Request 

class MetaCrawl(CrawlSpider): 
    name = 'metaspider' 
    start_urls = ['http://example.com/tools/rss'] # urls from which the spider will start crawling 
    rules = [Rule(SgmlLinkExtractor(allow=[r'.*',], restrict_xpaths=('//td[@class="xmlLink"]')), follow=True), 
      Rule(SgmlLinkExtractor(allow=[r'.*'], restrict_xpaths=('//h4[@class="itemtitle"]')),follow=True),] 


    def parse(self, response):  
     hxs = HtmlXPathSelector(response) 
     meta = hxs.select('//td[@class="xmlLink"]/a/@href') 
     for m in meta: 
      yield Request(m.extract(), callback = self.parse_link) 


    def parse_link(self, response):  
     hxs = HtmlXPathSelector(response) 
     meta = hxs.select('//h4[@class="itemtitle"]/a/@href') 
     for m in meta: 
      yield Request(m.extract(), callback = self.parse_again)  

    def parse_again(self, response): 
     hxs = HtmlXPathSelector(response) 
     meta = hxs.select('//meta') 
     items = [] 
     for m in meta: 
      item = exampleItem() 
      item['link'] = response.url 
      item['meta_name'] = m.select('@name').extract() 
      item['meta_value'] = m.select('@content').extract() 
      items.append(item) 
     return items 
+0

Tôi đã cố chỉnh sửa các quy tắc là: Quy tắc (SgmlLinkExtractor (allow = [r '. *'], Limits_xpaths = ('// td [@ class = "xmlLink"]')), follow = True), Quy tắc (SgmlLinkExtractor (cho phép = [r '. *'], Limits_xpaths = ('// h4 [@ class = "itemtitle"]')), callback = 'parse_articles') Nhưng vẫn dẫn đến kết quả tương tự đầu ra. – Marc

+0

Xin chào Marc: cuối cùng bạn đã xử lý vấn đề đó như thế nào? Khi tôi chạy các ví dụ rõ ràng mọi thứ đều ổn và khi logic được áp dụng cho dự án của tôi có vẻ như các quy tắc không bao giờ được đưa ra ... – hugsbrugs

+0

chỉ thừa kế con nhện của bạn từ 'scrapy.Spider' thực sự dễ dàng hơn nhiều. – lhe

Trả lời

2

Bạn đã trả lại một trống items, bạn cần phải thêm item để items.
Bạn cũng có thể yield item trong vòng lặp.

+0

Có, bạn đúng tôi quên đặt "items.append (item)" vào bài đăng. Tuy nhiên nó vẫn cho tôi cùng một đầu ra. Tôi sẽ chỉnh sửa ngay bây giờ. – Marc

Các vấn đề liên quan