wnj
wnj

Reputation: 57

ValueError: Missing scheme in request url: h 5

I use Scrapy to write a Spider to get image from a website.But when I run this Spider ,raise this error.Here is my code about get img_url:

img_url = div.find_all("img",class_="img-responsive img-thumbnail center-block")[0]['src']

When I put the img_url into browser,I can get the image.But when I download the images by spider,it raises the error.

  File "C:\Python27\lib\site-packages\scrapy\http\request\__init__.py", line 57,
 in _set_url
    raise ValueError('Missing scheme in request url: %s' % self._url)
ValueError: Missing scheme in request url: h

spider.py

# -*- coding: utf-8 -*-

from scrapy.spiders import Spider
import scrapy
from scrapy.selector import Selector
from bs4 import BeautifulSoup
from deep_web2.items import DeepWeb2Item

import sys
reload(sys)
sys.setdefaultencoding('utf8')


class DeepSpider(Spider):
    name = "deepSpider"
    staer_urls=["http://hansamktkykr5yt4.onion/category/1/"]
    bash_url = "http://hansamktkykr5yt4.onion"
    headers = {
        "Host": "hansamktkykr5yt4.onion",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
        "Connection": "keep-alive"
    }

    def start_requests(self):
        yield scrapy.Request(url="http://hansamktkykr5yt4.onion/category/1/",headers=self.headers,
                             callback=self.parse_item)

    def parse_item(self, response):
        sel = Selector(response)

        html = sel.extract()
        html = html.encode('utf-8')

        soup = BeautifulSoup(html,"lxml")

        item_rows  = soup.find_all("div",class_="row row-item")
        for div in item_rows:
            title = div.find_all("div",class_="item-details")[0].find_all("a")[0].get_text()
            url = div.find_all("div",class_="item-details")[0].find_all("a")[0]['href']
            address = div.find_all("small",class_="text-muted-666")[0].get_text()
            price = div.find_all("div",class_="col-xs-3 text-right listing-price")[0].find_all("strong")[0].get_text()

            img_url = div.find_all("img",class_="img-responsive img-thumbnail center-block")[0]['src']
            view_num =div.find_all("div",class_="text-muted text-center")[0].find_all("small")[0].get_text()

            link_ = self.bash_url+url
            yield scrapy.Request(url=link_,headers=self.headers,meta={"title":title,"address":address,
                                                                      "price":price,"img_url":img_url,
                                                                       "view_num":view_num},callback=self.parse_fetch)

        pageNum = soup.find_all("ul",class_="pagination")[0]
        now = pageNum.find_all("li",class_="active")[0].get_text()
        now = int(str(now).strip())
        print now
        for page_ in pageNum.find_all("li",class_=''):
            number_ = page_.get_text()
            try:
                temp = int(str(number_).strip())
            except:
                    continue
            page_next = int(str(number_).strip())
            if page_next==now+1:
                url = self.bash_url+page_.find_all("a")[0]['href']
                yield scrapy.Request(url=url,headers=self.headers,callback=self.parse_item)

    def parse_fetch(self, response):
        sel = Selector(response)

        html = sel.extract()
        html = html.encode('utf-8')

        soup = BeautifulSoup(html,"lxml")
        text = soup.find_all("p")[0].get_text()

        item = DeepWeb2Item()

        item['title'] = response.meta['title']
        item['address'] = response.meta['address']
        item['price'] = response.meta['price']
        item['img_url'] = response.meta['img_url']
        item['view_num'] = response.meta['view_num']
        item['content'] = text

        yield item

more error of information is here:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 587, in _
runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "C:\Python27\lib\site-packages\scrapy\pipelines\media.py", line 62, in pr
ocess_item
    requests = arg_to_iter(self.get_media_requests(item, info))
  File "C:\Python27\lib\site-packages\scrapy\pipelines\images.py", line 147, in
get_media_requests
    return [Request(x) for x in item.get(self.images_urls_field, [])]
  File "C:\Python27\lib\site-packages\scrapy\http\request\__init__.py", line 25,
 in __init__
    self._set_url(url)
  File "C:\Python27\lib\site-packages\scrapy\http\request\__init__.py", line 57,
 in _set_url
    raise ValueError('Missing scheme in request url: %s' % self._url)
ValueError: Missing scheme in request url: h
2017-03-15 08:42:23 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://hansam
ktkykr5yt4.onion/listing/63776/> (referer: http://hansamktkykr5yt4.onion/categor
y/1/)
2017-03-15 08:42:23 [scrapy.core.scraper] ERROR: Error processing {'address': u'
 Ships from: Netherlands',

Upvotes: 1

Views: 735

Answers (1)

Maxime de Pachtere
Maxime de Pachtere

Reputation: 283

Your spider start_urls must be a list as:

start_urls = ["https://www.google.com/"]

Actually, your string is interpreted as a list of char and when spider try to get the first element, it get the first letter "h".

Upvotes: 1

Related Questions