0

I am getting this error in scrapy framework. This is my dmoz.py under spiders directory:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from dirbot.items import Website


class DmozSpider(BaseSpider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    f = open("links.csv")
    start_urls = [url.strip() for url in f.readlines()]
    f.close()
    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//ul/li')
        items = []

        for site in sites:
            item = Website()
            item['name'] = site.select('a/text()').extract()
            item['url'] = site.select('a/@href').extract()
            item['description'] = site.select('text()').extract()
            items.append(item)

        return items

I am getting this error while running this code:

<GET %22http://www.astate.edu/%22>: Unsupported URL scheme '': no handler available for that scheme in Scrapy

Here's my content of links.csv:

http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/
http://www.atsu.edu/

There are 80 URLs in links.csv. How can I resolve this error?

Joyfulgrind
  • 2,762
  • 8
  • 34
  • 41
  • As a side note you should move the CSV file reading outside the attribute declaration section, perhaps in a tiny static or class method (I'm not familiar with scrapy). Also have a look at the answers for [this question](http://stackoverflow.com/questions/9322219/how-to-generate-the-start-urls-dynamiclly-in-crawling), which suggest overriding the `start_requests` method. – Cristian Ciupitu Nov 08 '12 at 10:26

1 Answers1

4

%22 is " urlencoded. Your CSV file probably has lines like this:

"http://example.com/"
  1. Use the csv module to read the file, OR
  2. strip the "s.

Edit: As requested:

'"http://example.com/"'.strip('"')

Edit 2:

import csv
from StringIO import StringIO

c = '"foo"\n"bar"\n"baz"\n'      # Since csv.reader needs a file-like-object,
reader = csv.reader(StringIO(c)) # wrap c into a StringIO.
for line in reader:
    print line[0]

LAST Edit:

import csv

with open("links.csv") as f:
    r = csv.reader(f)
    start_urls = [l[0] for l in r]