Python和Scrapy的递归刮擦:未检索到的信息

时间:2014-03-10 21:02:29

标签: python scrapy

我正在尝试使用Scrapy从Pratt网站提取联系信息,但未检索到信息。我的代码如下:

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

class ESpider(CrawlSpider):
    name = "pratt"
    allowed_domains = ["pratt.edu"]
    start_urls = ["https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302"]

    rules = (Rule (SgmlLinkExtractor(restrict_xpaths=('/html/body/div[3]/div/div[2]/div/div/p/a',))
    , callback="parse_items", follow= True),
    )

    def parse_items(self, response):
        contacts = Selector(response)
        print contacts.xpath('/html/body/div[3]/div/div[2]/table/tbody/tr[2]/td[2]/h3').extract()
        print contacts.xpath('/html/body/div[3]/div/div[2]/table/tbody/tr[2]/td[2]/a').extract()

start_url开始,我想浏览每个人的链接,并从下一页获取他们的姓名和电子邮件地址。当我运行我的刮刀时,我收到以下输出:

2014-03-10 16:46:37-0400 [scrapy] INFO: Scrapy 0.22.2 started (bot: emailSpider)
2014-03-10 16:46:37-0400 [scrapy] INFO: Optional features available: ssl, http11
2014-03-10 16:46:37-0400 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'emailSpider.spiders', 'SPIDER_MODULES': ['emailSpider.spiders'], 'DEPTH_LIMIT': 1, 'BOT_NAME': 'emailSpider'}
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-03-10 16:46:37-0400 [scrapy] INFO: Enabled item pipelines: 
2014-03-10 16:46:37-0400 [pratt] INFO: Spider opened
2014-03-10 16:46:37-0400 [pratt] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2014-03-10 16:46:37-0400 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2014-03-10 16:46:37-0400 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2014-03-10 16:46:41-0400 [pratt] DEBUG: Crawled (200) <GET https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302> (referer: None)
2014-03-10 16:46:44-0400 [pratt] DEBUG: Crawled (200) <GET https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/bio/?id=eabruzzo> (referer: https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302)
[]
[]
2014-03-10 16:46:47-0400 [pratt] DEBUG: Crawled (200) <GET https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/bio/?id=ehinrich> (referer: https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302)
[]
[]

(经过几次迭代后我暂停了程序)。看起来页面正在被删除,但是返回了空列表。知道为什么会这样吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

您的xpath不正确。实际上,它们非常脆弱,尽量避免在爬虫中使用绝对xpath。更好地找到具有唯一ID或类名的容器并依赖它。

以下是您的蜘蛛的固定版本(依赖div id="content"):

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.item import Item, Field
from scrapy.selector import Selector


class PrattItem(Item):
    name = Field()
    email = Field()


class ESpider(CrawlSpider):
    name = "pratt"
    allowed_domains = ["pratt.edu"]
    start_urls = ["https://www.pratt.edu/academics/architecture/ug_dept_architecture/faculty_and_staff/?id=01302"]

    rules = (Rule(SgmlLinkExtractor(restrict_xpaths=('//div[@id="content"]',)), callback="parse_items", follow=True), )

    def parse_items(self, response):
        contacts = Selector(response)

        item = PrattItem()
        item['name'] = contacts.xpath('//div[@id="content"]/table//h2/text()').extract()[0]
        item['email'] = contacts.xpath('//div[@id="content"]/table/tr[2]/td[2]/a/@href').extract()[0]
        return item

希望有所帮助。