Я пишу паук для сканирования для сегодняшних статей NYT с домашней страницы, но по какой-то причине он не следует никаким ссылкам. Когда я создаю экземпляр link extractor в scrapy shell http://www.nytimes.com
, он успешно извлекает список URL-адресов статьи с le.extract_links(response)
, но я не могу получить команду crawl (scrapy crawl nyt -o out.json
), чтобы очистить все, кроме главной страницы. Я вроде как в конце своего ума. Это потому, что домашняя страница не дает статьи из функции синтаксического анализа? Любая помощь приветствуется.Scrapy Spider не следует за ссылками
from datetime import date
import scrapy
from scrapy.contrib.spiders import Rule
from scrapy.contrib.linkextractors import LinkExtractor
from ..items import NewsArticle
with open('urls/debug/nyt.txt') as debug_urls:
debug_urls = debug_urls.readlines()
with open('urls/release/nyt.txt') as release_urls:
release_urls = release_urls.readlines() # ["http://www.nytimes.com"]
today = date.today().strftime('%Y/%m/%d')
print today
class NytSpider(scrapy.Spider):
name = "nyt"
allowed_domains = ["nytimes.com"]
start_urls = release_urls
rules = (
Rule(LinkExtractor(allow=(r'/%s/[a-z]+/.*\.html' % today,)),
callback='parse', follow=True),
)
def parse(self, response):
article = NewsArticle()
for story in response.xpath('//article[@id="story"]'):
article['url'] = response.url
article['title'] = story.xpath(
'//h1[@id="story-heading"]/text()').extract()
article['author'] = story.xpath(
'//span[@class="byline-author"]/@data-byline-name'
).extract()
article['published'] = story.xpath(
'//time[@class="dateline"]/@datetime').extract()
article['content'] = story.xpath(
'//div[@id="story-body"]/p//text()').extract()
yield article