老师您好,我的爬虫好像没有结果

来源:4-13 提取详情页信息

weixin_慕的地2398265

2020-01-01

老师您好,请问我的爬虫没有结果是为什么?按照您在视频那样子debug,没有结果代码和错误在下面。谢谢老师

-- coding: utf-8 --

from urllib import parse
import re
import json

import scrapy
from scrapy import Request
import requests

class JobboleSpider(scrapy.Spider):
name = 'jobbole’
allowed_domains = [‘https://www.cnblogs.com/news/’]
start_urls = [‘https://www.cnblogs.com/news/’]

def parse(self, response):
    #url = response.xpath(' // *[ @ id = "post_list"] / div[1] / div[2] / h3 / a/@href').extract_first("")
    ''''// *[ @ id = "post_list"] / div[1] / div[2] / h3 / a'''
    '''获取新闻列表url交给scrapy进行下载调用相应的解析方法
    获取下一页url并交给scrapy进行下载
    '''

    post_nodes = response.css('#post_list .post_item')[:1]
    for post_node in post_nodes:
        image_url = post_node.css('.entry_summary a img::attr(src)').extract_first("")
        post_url = post_node.css("h2 a::attr(href)").extract_first("")
        yield Request(url=parse.urljoin(response.url, post_url), meta={"front_image_url": image_url},
                      callback=self.parse_detail)

    #next_url = response.css("div.pager a:last-child::text").extract_first("")
    next_url = response.xpath("//a[contains(text(),'Next >')]/@href").extract_first("")
    yield Request(url=parse.urljoin(response.url, next_url), callback=self.parse)
    '''if next_url=='Next >':
        next_url = response.css("div.pager a:last-child::attr(href)").extract_first("")'''


def parse_detail(self, response):
    match_re = re.match(".*?(\d+)",response.url)
    if match_re:
        title = response.css("#news_title a::text").extract_first("")
        creat_date = response.css("#news_info .time::text").extract_first("")
        content = response.css("#news_content").extract()[0]
        tag_list = response.css("#news_more_info a::text").extract()
        tag = ",".join(tag_list)

        post_id = match_re.group(1)
        html = requests.get(parse.urljoin(response.url, "NewsAjax/GetAjaxNewsInfo?contentId={}".format(post_id)))
        j_data = json.loads(html.text)

        #yield Request(url=parse.urljoin(response.url, "NewsAjax/GetAjaxNewsInfo?contentId={}".format(post_id)), callback=self.parse)

        praise_nums = j_data['DiggCount']
        fav_nums = j_data['TotalView']
        connment_nums = j_data['CommentCount']
        pass

D:\anaconda\python.exe “D:\pycharm\PyCharm 2019.3.1\plugins\python\helpers\pydev\pydevd.py” --multiproc --qt-support=auto --client 127.0.0.1 --port 54072 --file E:/pythonproject/ArticleSpider/main.py
pydev debugger: process 9308 is connecting

Connected to pydev debugger (build 193.5662.61)
2020-01-01 20:26:33 [scrapy.utils.log] INFO: Scrapy 1.8.0 started (bot: ArticleSpider)
2020-01-01 20:26:33 [scrapy.utils.log] INFO: Versions: lxml 4.4.1.0, libxml2 2.9.9, cssselect 1.1.0, parsel 1.5.2, w3lib 1.21.0, Twisted 19.10.0, Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)], pyOpenSSL 19.0.0 (OpenSSL 1.1.1d 10 Sep 2019), cryptography 2.7, Platform Windows-10-10.0.17763-SP0
2020-01-01 20:26:33 [scrapy.crawler] INFO: Overridden settings: {‘BOT_NAME’: ‘ArticleSpider’, ‘NEWSPIDER_MODULE’: ‘ArticleSpider.spiders’, ‘ROBOTSTXT_OBEY’: True, ‘SPIDER_MODULES’: [‘ArticleSpider.spiders’]}
2020-01-01 20:26:33 [scrapy.extensions.telnet] INFO: Telnet Password: e3ffef98b44c5bb0
2020-01-01 20:26:34 [scrapy.middleware] INFO: Enabled extensions:
[‘scrapy.extensions.corestats.CoreStats’,
‘scrapy.extensions.telnet.TelnetConsole’,
‘scrapy.extensions.logstats.LogStats’]
2020-01-01 20:26:34 [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’]
2020-01-01 20:26:34 [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’]
2020-01-01 20:26:34 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2020-01-01 20:26:34 [scrapy.core.engine] INFO: Spider opened
2020-01-01 20:26:34 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-01-01 20:26:34 [py.warnings] WARNING: D:\anaconda\lib\site-packages\scrapy\spidermiddlewares\offsite.py:61: URLWarning: allowed_domains accepts only domains, not URLs. Ignoring URL entry https://www.cnblogs.com/news/ in allowed_domains.
warnings.warn(message, URLWarning)

2020-01-01 20:26:34 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-01-01 20:26:34 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.cnblogs.com/robots.txt> (referer: None)
2020-01-01 20:26:34 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.cnblogs.com/news/> (referer: None)
2020-01-01 20:26:34 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to ‘www.cnblogs.com’: <GET https://www.cnblogs.com/news/>
2020-01-01 20:26:34 [scrapy.core.engine] INFO: Closing spider (finished)
2020-01-01 20:26:34 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{‘downloader/request_bytes’: 443,
‘downloader/request_count’: 2,
‘downloader/request_method_count/GET’: 2,
‘downloader/response_bytes’: 14751,
‘downloader/response_count’: 2,
‘downloader/response_status_count/200’: 2,
‘elapsed_time_seconds’: 0.469347,
‘finish_reason’: ‘finished’,
‘finish_time’: datetime.datetime(2020, 1, 1, 12, 26, 34, 632634),
‘log_count/DEBUG’: 3,
‘log_count/INFO’: 10,
‘log_count/WARNING’: 1,
‘offsite/domains’: 1,
‘offsite/filtered’: 2,
‘request_depth_max’: 1,
‘response_received_count’: 2,
‘robotstxt/request_count’: 1,
‘robotstxt/response_count’: 1,
‘robotstxt/response_status_count/200’: 1,
‘scheduler/dequeued’: 1,
‘scheduler/dequeued/memory’: 1,
‘scheduler/enqueued’: 1,
‘scheduler/enqueued/memory’: 1,
‘start_time’: datetime.datetime(2020, 1, 1, 12, 26, 34, 163287)}
2020-01-01 20:26:34 [scrapy.core.engine] INFO: Spider closed (finished)

Process finished with exit code 0

写回答

1回答

bobby

2020-01-03

这里看起来是被过滤掉了 你试试在yield的Request的参数中加上 dont_filter=True试试

0
7
bobby
回复
卷心菜菜芯
已加你了
2021-05-12
共7条回复

Scrapy打造搜索引擎 畅销4年的Python分布式爬虫课

带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎

5796 学习 · 6290 问题

查看课程