问答详情
源自:2-7 spider文件的编写(2)

老师问一下,response.text可以打印出top250页面。但是i_item无法打印,可能是什么原因造成的?

# -*- coding: utf-8 -*-
import scrapy
from  douban.items import DoubanItem


class DoubanSpiderSpider(scrapy.Spider):
    #这素爬虫名称
    name = "douban_spider"
    #容许的域名
    allowed_domains = ["movie.douban.com"]
    #入口url,扔到调度器里面去
    start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        #print(response.text)
        movie_list = response.xpath("//div[@class='article']//ol[@class='gril_view']/li/")
        # print(movie_list)
        for i_item in movie_list:
             # douban_item = DoubanItem()
             # douban_item['serial_number'] = i_item.xpath(".//div[@class='item']//em/text()").extract_first()
             print(i_item)
我用的环境是python3.7。


提问者:慕沐0348027 2018-08-10 07:59

个回答

  • qq_客欲远行_0
    2018-08-10 12:15:11
    已采纳

    贴一下完整代码一起看看

  • 李妏
    2019-05-04 15:55:20

    后面多了个/


  • qq_客欲远行_0
    2018-08-11 10:51:39

    打印的结果中没有报错,只是没有爬取到内容,这种情况一般检查代码中的url入口,xpath的路径信息。因为任何内容都没有爬取到,所以应该是根节点的问题,看这行代码

    movie_list = response.xpath("//div[@class='article']//ol[@class='gril_view']/li/")

    gril_view 写错了,改一下试试能不能解决问题

  • 慕沐0348027
    2018-08-10 21:43:11

    C:\Users\84199\AppData\Local\Programs\Python\Python37\python.exe D:/scrapy/douban/douban/main.py
    2018-08-10 21:41:52 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: douban)
    2018-08-10 21:41:52 [scrapy.utils.log] INFO: Versions: lxml 4.2.4.0, libxml2 2.9.5, cssselect 1.0.3, parsel 1.5.0, w3lib 1.19.0, Twisted 18.7.0, Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.0h  27 Mar 2018), cryptography 2.3, Platform Windows-10-10.0.17134-SP0
    2018-08-10 21:41:52 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'douban', 'NEWSPIDER_MODULE': 'douban.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['douban.spiders'], 'USER_AGENT': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Mobile Safari/537.36'}
    2018-08-10 21:41:52 [scrapy.middleware] INFO: Enabled extensions:
    ['scrapy.extensions.corestats.CoreStats',
     'scrapy.extensions.telnet.TelnetConsole',
     'scrapy.extensions.logstats.LogStats']
    2018-08-10 21:41:52 [scrapy.middleware] INFO: Enabled downloader middlewares:
    ['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
     'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
     'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
     'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
     'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
     'scrapy.downloadermiddlewares.retry.RetryMiddleware',
     'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
     'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
     'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
     'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
     'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
     'scrapy.downloadermiddlewares.stats.DownloaderStats']
    2018-08-10 21:41:52 [scrapy.middleware] INFO: Enabled spider middlewares:
    ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
     'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
     'scrapy.spidermiddlewares.referer.RefererMiddleware',
     'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
     'scrapy.spidermiddlewares.depth.DepthMiddleware']
    2018-08-10 21:41:52 [scrapy.middleware] INFO: Enabled item pipelines:
    []
    2018-08-10 21:41:52 [scrapy.core.engine] INFO: Spider opened
    2018-08-10 21:41:52 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
    2018-08-10 21:41:52 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
    2018-08-10 21:41:53 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://movie.douban.com/robots.txt> (referer: None)
    2018-08-10 21:41:53 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://movie.douban.com/top250> (referer: None)
    []
    2018-08-10 21:41:53 [scrapy.core.engine] INFO: Closing spider (finished)
    2018-08-10 21:41:53 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
    {'downloader/request_bytes': 646,
     'downloader/request_count': 2,
     'downloader/request_method_count/GET': 2,
     'downloader/response_bytes': 13402,
     'downloader/response_count': 2,
     'downloader/response_status_count/200': 2,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2018, 8, 10, 13, 41, 53, 345844),
     'log_count/DEBUG': 3,
     'log_count/INFO': 7,
     'response_received_count': 2,
     'scheduler/dequeued': 1,
     'scheduler/dequeued/memory': 1,
     'scheduler/enqueued': 1,
     'scheduler/enqueued/memory': 1,
     'start_time': datetime.datetime(2018, 8, 10, 13, 41, 52, 902343)}
    2018-08-10 21:41:53 [scrapy.core.engine] INFO: Spider closed (finished)

    进程已结束,退出代码0

    这是打印的结果