Próbuję zaprogramować pająka przeszukiwania do przeszukiwania kanałów RSS witryny, a następnie analizowania metatagów artykułu.Jak prawidłowo używać reguł, restrict_xpaths do przeszukiwania i analizowania adresów URL za pomocą scrapy?
Pierwsza strona RSS to strona z kategoriami RSS. Udało mi się wyodrębnić link, ponieważ tag znajduje się w tagu. Wygląda to tak:
<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>
Po kliknięciu tego łącza to przynosi artykułów dla tej kategorii RSS, który wygląda tak:
<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>
Jak widać można uzyskać połączenie z XPath ponownie, jeśli użyję tagu , chcę, aby mój robot indeksujący podszedł do linku wewnątrz tego znacznika i przeanalizował dla mnie metatagi.
Oto mój kod gąsienicowy:
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
Jednak jest to wyjście, gdy uruchomię gąsienicowe:
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)
Co robię źle tutaj? Czytałem dokumentację w kółko, ale czuję, że ciągle coś przeoczam. Każda pomoc będzie doceniona.
EDYCJA: Dodano: items.append (pozycja). Zapomniałem o tym w oryginalnym poście. EDIT:: Próbowałem to jak dobrze i to spowodowało w tym samym wyjścia:
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
Próbowałem edytować reguły: Reguła (SgmlLinkExtractor (allow = [r '. *'], Restrict_xpaths = ('// td [@ class = "xmlLink"]')), follow = True), Reguła (SgmlLinkExtractor (allow = [r '. *'], Restrict_xpaths = ('// h4 [@ class = "itemtitle"]')), callback = 'parse_articles') Ale to wciąż skutkowało tym samym wydajność. – Marc
Cześć Marc: jak w końcu poradziłeś sobie z tym problemem? Kiedy uruchamiam przykłady scrapy, wszystko jest w porządku, a kiedy logika zastosowana do mojego projektu wydaje się, że reguły nigdy się nie uruchamiają ... – hugsbrugs
po prostu odziedzicz swój pająk z 'scrapy.Spider' jest w rzeczywistości znacznie łatwiejszy. – lhe