Scrapy simulate XHR请求返回400

2024-06-01 10:54:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Ajax从站点获取数据。页面加载,然后Javascript请求内容。有关详细信息,请参见本页:https://www.tele2.no/mobiltelefon.aspx

问题是,当我试图通过调用此url来模拟此过程时: https://www.tele2.no/Services/Webshop/FilterService.svc/ApplyPhoneFilters

我得到了400个回复,告诉我这个请求是不允许的。 这是我的代码:

# -*- coding: utf-8 -*-
import scrapy
import json

class Tele2Spider(scrapy.Spider):
    name = "tele2"
    #allowed_domains = ["tele2.no/mobiltelefon.aspx"]
    start_urls = (
        'https://www.tele2.no/mobiltelefon.aspx/',
    )

    def parse(self, response):
        url = 'https://www.tele2.no/Services/Webshop/FilterService.svc/ApplyPhoneFilters'
        my_data = "{filters: []}"
        req = scrapy.Request( url, method='POST', body=json.dumps(my_data), headers={'X-Requested-With': 'XMLHttpRequest','Content-Type':'application/json'}, callback=self.parser2)
        yield req

    def parser2(self, response):
      print "test"

我对scrapy和python还不太熟悉,所以我可能遗漏了一些显而易见的东西


Tags: nohttpsimportselfjsonurldefwww