2014-01-18 1 views
0

저는 scrapy를 사용하여 m-ati.su 용 파서를 작성하려고합니다. 첫 번째 단계에서는 다른 도시의 "From"과 "To"라는 이름의 콤보 박스에서 값과 텍스트 필드를 가져와야합니다. 나는 방화 광에서 요청한 것을 보았고아약스와 함께 combobox에서 가치와 텍스트 필드를 얻는 방법?

class spider(BaseSpider): 
    name = 'ati_su' 
    start_urls = ['http://m-ati.su/Tables/Default.aspx?EntityType=Load'] 
    allowed_domains = ["m-ati.su"] 

    def parse(self, response): 
     yield FormRequest('http://m-ati.su/Services/ATIGeoService.asmx/GetGeoCompletionList', 
         callback=self.ati_from, 
         formdata={'prefixText': 'moscow', 'count': '10','contextKey':'All_0$Rus'}) 
    def ati_from(self, response): 
     json = response.body 
     open('results.txt', 'wb').write(json) 

이 요청에 대해 "500 내부 서버 오류"가 있습니다. 나는 무엇을 잘못 했는가? 나쁜 영어로 죄송합니다. 감사

답변

0

나는 당신이 당신의 POST 요청에 X-Requested-With: XMLHttpRequest 헤더를 추가 할 수 있습니다 생각, 그래서 당신이 시도 할 수 있습니다 :

def parse(self, response): 
     yield FormRequest('http://m-ati.su/Services/ATIGeoService.asmx/GetGeoCompletionList', 
          callback=self.ati_from, 
          formdata={'prefixText': 'moscow', 'count': '10','contextKey':'All_0$Rus'}, 
          headers={"X-Requested-With": "XMLHttpRequest"}) 

편집 : 나는 거미를 실행하는 노력이와 함께 :

(요청 본문이 나는 그래서 Request 및 강제 "POST"방법을 사용 파이어 폭스와 함께 검사 할 때 JSON 인코딩, 그리고 내가 가진 응답이 "창-1251"에 endoded되었다)

,
from scrapy.spider import BaseSpider 
from scrapy.http import Request 
import json 

class spider(BaseSpider): 
    name = 'ati_su' 
    start_urls = ['http://m-ati.su/Tables/Default.aspx?EntityType=Load'] 
    allowed_domains = ["m-ati.su"] 

    def parse(self, response): 
     yield Request('http://m-ati.su/Services/ATIGeoService.asmx/GetGeoCompletionList', 
         callback=self.ati_from, 
         method="POST", 
         body=json.dumps({ 
          'prefixText': 'moscow', 
          'count': '10', 
          'contextKey':'All_0$Rus' 
         }), 
         headers={ 
          "X-Requested-With": "XMLHttpRequest", 
          "Accept": "application/json, text/javascript, */*; q=0.01", 
          "Content-Type": "application/json; charset=utf-8", 
          "Pragma": "no-cache", 
          "Cache-Control": "no-cache", 
         }) 
    def ati_from(self, response): 
     jsondata = response.body 
     print json.loads(jsondata, encoding="windows-1251") 
+0

FormReqest [doc.scrapy] (http://doc.scrapy.org/en/latest/topics/request-response.html#formrequest-objects)에 헤더 매개 변수가 없습니다. – yavalvas

+0

"FormRequest 클래스가 기본 요청을 확장"하므로 'headers' 매개 변수를 사용할 수 있습니다. 시도해 보았 니? –

+0

Ah, sry. 나는 그 오류를 다시 시도했다. – yavalvas

관련 문제