Scrapy:未提供来源或此服务不允许

Scrapy: origin not supplied or is not allowed by this service

所以我尝试抓取目标产品的评分,它使用 ajax。我发现 XHR 的 URL 是: http://tws.target.com/productservice/services/reviews/v1/reviewstats/21499594

现在是让我困惑的部分: 1. 如果我去 target.com,搜索那个特定的产品,打开它,然后去上面提到的 URL,我可以在我的浏览器中看到 json 文件 2. 但是如果我直接在浏览器中输入 URL,我会收到一条错误消息 "origin not supplied or is not allowed by this service."

请问是什么原因造成的,我该如何解决? 谢谢你! (顺便说一句,我正在使用scrapy)

我找到了一个可行的解决方案,但如果有人能告诉我底层机制那就太棒了:)

所以在我的请求中,我添加了一个 headers 说我来自 target.com ('Origin': 'http://www.target.com'),然后一切正常

head = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 '
                  '(KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36',
                'Origin': 'http://www.target.com'
       }

jsoncontent = requests.get(url, headers=head).content
jsdict = json.loads(jsoncontent)