我正在尝试使用scrapy从本地http服务器中废弃html文件,但是我收到此错误
属性错误:"unicode"对象没有属性"css">
这是代码
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
如果你打印response.css('.listing.listing-search.listing-data').extract()
你会为什么得到这个错误,.extract()
实际上返回一个str/unicode项的列表,如果你想在返回的列表项上使用css/xpath
,请不要使用extract()
,因为它会返回Selector
类的对象。
请参阅下面的代码,
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