2017-12-10 6 views
-6

방금 ​​치료으로 시작했습니다. 나는 scrapy shell [url]있는 페이지 http://www.ikea.com/ae/en/catalog/categories/departments/childrens_ikea/31772/로드 및 제품 이름을 얻을 수 response.css(div.productTitle.Floatleft)를 실행하지만 나에게 다음과 같은 오류를 제공했습니다reponse.css on not working

Traceback (most recent call last): File "", line 1, in NameError: name 'div' is not defined.

어떻게이 문제를 해결할 수 있습니까?

+0

. 이제 변수'div'를 사용하려고 시도했습니다. – furas

답변

0

문자열 : "div.productTitle.Floatleft"을 사용해야합니다. " "

이제 div 변수를 사용해보십시오.


편집 :User-Agent

실행하면 서버에서 HTML을보기 위해 웹 브라우저를 사용할 수있는 쉘에서

scrapy shell http://www.ikea.com/ae/en/catalog/categories/departments/childrens_ikea/31772/ 

쉘 사용자가 설정해야 정확한 데이터를 얻을 당신이 오류가 발생합니다 메시지.

view(response) 

당신은 페이지를 다시 BTW

fetch(response.url, headers={'User-Agent': 'Mozilla/5.0'}) 

response.css('div.productTitle.floatLeft') 

(이전 response에서 사용 url) 다른 User-Agent을 사용하여 얻을 : 그것은 floatLeft하지 Floatleft한다 - 낮은 볼 f 상부 L


편집 : 같은 독립형 스크립트로 (필요하지 않습니다 프로젝트) 파일 output.csv에서

import scrapy 

class MySpider(scrapy.Spider): 

    name = 'myspider' 

    #allowed_domains = ['http://www.ikea.com'] 

    start_urls = ['http://www.ikea.com/ae/en/catalog/categories/departments/childrens_ikea/31772/'] 

    def parse(self, response): 
     print('url:', response.url) 

     all_products = response.css('div.product') 

     for product in all_products: 
      title = product.css('div.productTitle.floatLeft ::text').extract() 
      description = product.css('div.productDesp ::text').extract() 
      price = product.css('div.price.regularPrice ::text').extract() 
      price = price[0].strip() 

      print('item:', title, description, price) 

      yield {'title': title, 'description': description, 'price': price} 

# --- it runs without project and saves in 'output.csv' --- 

from scrapy.crawler import CrawlerProcess 

c = CrawlerProcess({ 
    'USER_AGENT': 'Mozilla/5.0', 
    'FEED_FORMAT': 'csv', 
    'FEED_URI': 'output.csv', 
}) 
c.crawl(MySpider) 
c.start() 

결과 : "div.productTitle.Floatleft"당신이 문자열을 사용해야

title,description,price 
BÖRJA,feeding spoon and baby spoon,Dhs 5.00 
BÖRJA,training beaker,Dhs 5.00 
KLADD RANDIG,bib,Dhs 9.00 
KLADDIG,bib,Dhs 29.00 
MATA,4-piece eating set,Dhs 9.00 
SMASKA,bowl,Dhs 9.00 
SMASKA,plate,Dhs 12.00 
SMÅGLI,plate/bowl,Dhs 19.00 
STJÄRNBILD,bib,Dhs 19.00 
+0

안녕하세요, 많은 응답 주셔서 감사합니다. 나는 이것을 실제로 시험해 보았고 결과로 나에게 준다. 이걸로 나를 도울 수 있니? – user7105892

+0

은'scrap shell'에서'view (response)'를 사용하고 웹 브라우저에서 서버로부터 얻은 것을 볼 수 있습니다. 그것은 그것이 봇에 대한 에러 또는 어쩌면 메시지에 관한 메시지를 줄 것 같습니다. 어쩌면 올바른 데이터를 얻기 위해'User-Agent' 헤더 (또는 다른 헤더도)를 설정해야 할 것입니다. – furas

+0

'Mozilla/5.0'에 설정된'User-Agent'를 사용하여 검사했고 서버로부터 올바른 HTML을 제공합니다. 하지만 잘못된 CSS 선택기가 있습니다. FloatLeft가 아니라 Floatleft 여야합니다. 아래쪽 'f'와 위쪽 'L'을 참조하십시오. – furas