Scrapy“请求URL中缺少方案”

5
以下是我的代码-

以下是我的代码-

import scrapy
from scrapy.http import Request

class lyricsFetch(scrapy.Spider):
    name = "lyricsFetch"
    allowed_domains = ["metrolyrics.com"]


print "\nEnter the name of the ARTIST of the song for which you want the lyrics for. Minimise the spelling mistakes, if possible."
artist_name = raw_input('>')

print "\nNow comes the main part. Enter the NAME of the song itself now. Again, try not to have any spelling mistakes."
song_name = raw_input('>')


artist_name = artist_name.replace(" ", "_")
song_name = song_name.replace(" ","_")
first_letter = artist_name[0]
print artist_name
print song_name

start_urls = ["www.lyricsmode.com/lyrics/"+first_letter+"/"+artist_name+"/"+song_name+".html" ]

print "\nParsing this link\t "+ str(start_urls)

def start_requests(self):
    yield Request("www.lyricsmode.com/feed.xml")

def parse(self, response):

    lyrics = response.xpath('//p[@id="lyrics_text"]/text()').extract()

    with open ("lyrics.txt",'wb') as lyr:
        lyr.write(str(lyrics))

    #yield lyrics

    print lyrics

当我使用scrapy shell时,我可以得到正确的输出,但是每当我尝试使用scrapy crawl运行脚本时,就会出现ValueError错误。我做错了什么?我查看了这个网站和其他网站,但是没有找到解决方法。我从另一个问题中得到了通过yield请求的想法,但仍然无法解决问题。需要帮助吗?

我的追溯信息-

Enter the name of the ARTIST of the song for which you want the lyrics for. Minimise the spelling mistakes, if possible.
>bullet for my valentine

Now comes the main part. Enter the NAME of the song itself now. Again, try not to have any spelling mistakes.
>your betrayal
bullet_for_my_valentine
your_betrayal

Parsing this link        ['www.lyricsmode.com/lyrics/b/bullet_for_my_valentine/your_betrayal.html']
2016-01-24 19:58:25 [scrapy] INFO: Scrapy 1.0.3 started (bot: lyricsFetch)
2016-01-24 19:58:25 [scrapy] INFO: Optional features available: ssl, http11
2016-01-24 19:58:25 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'lyricsFetch.spiders', 'SPIDER_MODULES': ['lyricsFetch.spiders'], 'BOT_NAME': 'lyricsFetch'}
2016-01-24 19:58:27 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2016-01-24 19:58:28 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2016-01-24 19:58:28 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2016-01-24 19:58:28 [scrapy] INFO: Enabled item pipelines:
2016-01-24 19:58:28 [scrapy] INFO: Spider opened
2016-01-24 19:58:28 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-01-24 19:58:28 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-01-24 19:58:28 [scrapy] ERROR: Error while obtaining start requests
Traceback (most recent call last):
  File "C:\Users\Nishank\Miniconda2\lib\site-packages\scrapy\core\engine.py", line 110, in _next_request
    request = next(slot.start_requests)
  File "C:\Users\Nishank\Desktop\SNU\Python\lyricsFetch\lyricsFetch\spiders\lyricsFetch.py", line 26, in start_requests
    yield Request("www.lyricsmode.com/feed.xml")
  File "C:\Users\Nishank\Miniconda2\lib\site-packages\scrapy\http\request\__init__.py", line 24, in __init__
    self._set_url(url)
  File "C:\Users\Nishank\Miniconda2\lib\site-packages\scrapy\http\request\__init__.py", line 59, in _set_url
    raise ValueError('Missing scheme in request url: %s' % self._url)
ValueError: Missing scheme in request url: www.lyricsmode.com/feed.xml
2016-01-24 19:58:28 [scrapy] INFO: Closing spider (finished)
2016-01-24 19:58:28 [scrapy] INFO: Dumping Scrapy stats:
{'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 1, 24, 14, 28, 28, 231000),
 'log_count/DEBUG': 1,
 'log_count/ERROR': 1,
 'log_count/INFO': 7,
 'start_time': datetime.datetime(2016, 1, 24, 14, 28, 28, 215000)}
2016-01-24 19:58:28 [scrapy] INFO: Spider closed (finished)

将方案添加到您的URL中:http:// https://。顺便问一下,您的代码真的功能完整吗? - tintin
是的,这就是整个代码,现在在哪里添加"http://"部分? - starship9
2个回答

4

正如@tintin所说,你在URL中缺少http方案。Scrapy需要完全合格的URL才能处理请求。

据我所见,你在以下位置缺少了该方案:

start_urls = ["www.lyricsmode.com/lyrics/ ...

并且

yield Request("www.lyricsmode.com/feed.xml")

如果您正在从HTML内容中解析URL,则应使用urljoin确保您获得完全合格的URL,例如:

next_url = response.urljoin(href)

将 http:// 添加到 start_urls 中并没有起作用。我正在根据用户提供的输入生成一个 URL,生成的 URL 与实际网站上存在的 URL 完全匹配。我不明白我错在哪里! - starship9
@starship9 我假设你的代码缩进设置正确。你正在覆盖 start_requests 方法,所以 start_urls 的值不会被使用。 - R. Max

0
今天我也遇到了这个问题,URL通常有一个方案,这是非常常见的,比如HTTP、HTTPS等。
应该是你从start_url响应中提取的url没有HTTP、HTTPS,例如//list.jd.com/list.html
你应该在url中添加方案,应该是https://list.jd.com/list.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接