【发布时间】:2015-06-18 17:44:47
【问题描述】:
我正在编写一个爬虫爬虫来从主页上抓取今天的 NYT 文章,但由于某种原因,它没有跟随任何链接。当我在scrapy shell http://www.nytimes.com 中实例化链接提取器时,它成功地提取了带有le.extract_links(response) 的文章网址列表,但是我无法让我的抓取命令(scrapy crawl nyt -o out.json)抓取除主页之外的任何内容。我有点不知所措了。是因为主页没有从解析函数中产生文章吗?任何帮助是极大的赞赏。
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
【问题讨论】:
标签: python scrapy scrapy-spider