gpt4 book ai didi

python - 使用Scrapy爬取MIT OCW网站,但输出为空

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:51 24 4
gpt4 key购买 nike

我正在尝试使用以下代码从 MIT OCW 中删除类(class)信息:

import scrapy

class mitSpider(scrapy.Spider):
name = 'mitSpider'
start_urls = ['https://ocw.mit.edu/courses/']

def parse(self, response):
for url in response.css('ul li h4 a::attr("href")'):
yield scrapy.Request(response.url, self.parse_desc)

def parse_desc(self, response):
for course_desc in response.css('div#description p::text').extract():
yield{'Description': course_desc}

但我没有得到任何返回。我得到的输出是(我安装了 service_identity,但收到了消息):

:0 UserWarning: You do not have a working installation of the service_identity module: 
'cannot import name 'opentype''.
Please install it from <https://pypi.python.org/pypi/service_identity>
and make sure all of its dependencies are satisfied. Without the
service_identity module, Twisted can perform only rudimentary TLS client
hostname verification. :0: Many valid certificate/hostname mappings
may be rejected. 2018-03-21 17:40:14 [scrapy.utils.log]
INFO: Scrapy 1.5.0 started (bot: mitcrawler)
2018-03-21 17:40:14 [scrapy.utils.log] INFO:
Versions: lxml 3.7.2.0, libxml2 2.9.4, cssselect 1.0.3, parsel 1.4.0,
w3lib 1.19.0, Twisted 17.9.0, Python 3.6.0 |Anaconda custom (64-bit)|
(default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)], pyOpenSSL
16.2.0 (OpenSSL 1.0.2k 26 Jan 2017), cryptography 1.7.1, Platform Windows-
10-10.0.16299-SP0 2018-03-21 17:40:14 [scrapy.crawler]
INFO: Overridden settings: {'BOT_NAME': 'mitcrawler', 'NEWSPIDER_MODULE':
'mitcrawler.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES':
['mitcrawler.spiders']}
2018-03-21 17:40:14 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-03-21 17:40:14 [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-03-21 17:40:14 [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-03-21 17:40:14 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-03-21 17:40:14 [scrapy.core.engine] INFO: Spider opened
2018-03-21 17:40:14 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0
pages/min), scraped 0 items (at 0 items/min)
2018-03-21 17:40:14 [scrapy.extensions.telnet] DEBUG: Telnet console
listening on 127.0.0.1:6023
2018-03-21 17:40:14 [scrapy.core.engine] INFO: Closing spider (finished)
2018-03-21 17:40:14 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'finish_reason': 'finished',
'log_count/DEBUG': 1,
'log_count/INFO': 7,
'start_time': datetime.datetime(2018, 3, 21, 6, 40, 14, 762492)}
2018-03-21 17:40:14 [scrapy.core.engine] INFO: Spider closed (finished)

我尝试按原样运行 scrapy 网站上提供的示例代码,但它也不起作用。所以问题可能不是代码本身。但我无法弄清楚问题是什么。请帮忙!

最佳答案

您正在尝试重新抓取相同的 resonse.url,请尝试使用循环中的 url:

import scrapy

class mitSpider(scrapy.Spider):
name = 'mitSpider'
start_urls = ['https://ocw.mit.edu/courses/']

def parse(self, response):
for href in response.css('ul li h4 a::attr(href)'):
url = response.urljoin(href.extract())
yield scrapy.Request(url, self.parse_desc)

def parse_desc(self, response):
for course_desc in response.css('div#description p::text').extract():
yield{'Description': course_desc}

关于python - 使用Scrapy爬取MIT OCW网站,但输出为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49400067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com