Ad

AttributeError: 'unicode' Object Has No Attribute 'css'

- 1 answer

I'm trying to scrap a html file from local http server using scrapy but i'm getting this error

AttributeError: 'unicode' object has no attribute 'css'

here is the code

import scrapy
from ..items import YpauItem
class ypau(scrapy.Spider):
    name = 'ypau'
    #allowed_domains = ['http://localhost']
    start_urls = ['http://localhost/']

   def parse(self, response):
    selector = response.css('a::attr(href)').extract()
    for url in selector:
        urls = scrapy.Request(response.urljoin(url), callback=self.parsePage)
        yield urls
        print urls

   def parsePage(self, response):
    container = response.css('.listing.listing-search.listing-data').extract()
    for items in container:
        bname = items.css('listing-name::text').extract()

        item = YpauItem()
        item['Business_Name'] = bname
        yield item
Ad

Answer

If you print response.css('.listing.listing-search.listing-data').extract() you will why are you getting that error, .extract() actually returns a list of str/unicode items, if you want to use css/xpath on returned list items, please do not use extract() so it will return you objects of Selector class.

See below code,

container = response.css('.listing.listing-search.listing-data')
for items in container:
    bname = items.css('listing-name::text').extract()

    item = YpauItem()
    item['Business_Name'] = bname
    yield item
Ad
source: stackoverflow.com
Ad