简体   繁体   English

如何使用ajax从组合框获取值和文本字段?

[英]How to get value and textfield from combobox with ajax?

I'm trying to write a parser for m-ati.su by using of scrapy. 我正在尝试使用scrapy为m-ati.su编写解析器。 At the first step I have to get values and textfields from comboboxes with names "From" and "To" for different cities. 第一步,我必须从不同城市的名称分别为“ From”和“ To”的组合框中获取值和文本字段。 I looked request at firebug and wrote 我看着萤火虫的请求并写道

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)

And I have "500 Internal Server Error" for this request. 我对此请求有“ 500 Internal Server Error”。 What did I do wrong? 我做错了什么? Sorry for bad english. 对不起,英语不好。 Thanks 谢谢

I think you may have to add a X-Requested-With: XMLHttpRequest header to your POST request, so you can try this: 我认为您可能必须在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"})

Edit: I tried running the spider and came with this: 编辑:我尝试运行蜘蛛,并与此:

(the request body is JSON encoded when I inspect it with Firefox so I used Request and forcing "POST" method, and the response I got was endoded in "windows-1251") (当我使用Firefox检查请求正文时,请求主体是JSON编码的,因此我使用了Request并强制使用“ POST”方法,并且得到的响应被封装在“ windows-1251”中)

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM