Kickstarter GraphQL API 与 Scrapy 请求

Kickstarter GraphQL API with Scrapy Requests

我正在尝试为 kickstarter.com 中列出的某些项目提取特定数据。

Kickstarter.com 使用 GraphQL,我正在尝试制作 API 的副本,它与 Python 请求库一起使用,但是当我在 scrapy 请求中使用它时,它一直返回错误403.

我假设问题出在内容类型上,但我没有找到我应该使用的正确类型,请注意当我使用普通请求库时它的工作原理与此完全相同。

def start_requests(self):
    url = "https://www.kickstarter.com/graph"
    payload = json.dumps([
    {
        "operationName": "Campaign",
        "variables": {
        "slug": "leightonconnor/akashic-titan-blue-bolt"
        },
        "query": "query Campaign($slug: String!) {\n  project(slug: $slug) {\n    id\n    isSharingProjectBudget\n    risks\n    story(assetWidth: 680)\n    currency\n    spreadsheet {\n      displayMode\n      public\n      url\n      data {\n        name\n        value\n        phase\n        rowNum\n        __typename\n      }\n      dataLastUpdatedAt\n      __typename\n    }\n    environmentalCommitments {\n      id\n      commitmentCategory\n      description\n      __typename\n    }\n    __typename\n  }\n}\n"
    }
    ])
    headers = {
    'content-type': 'application/json',
    'x-csrf-token': 'AZsT67Z9s-LHZt6ZJXLSQWJlNdd7biKz2XDfFMkcYMZrNufH1OWoFhNBlXIvxCrxKRzV6l8bG_Z6QlcRoYMe_g',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
    'cookie': '_ksr_session=fc2U7qXXaRN91foNiE53NyU3s181NZO0Ll57xPkYxZ5iyUNgus35a0HwsPBTfViBY%2ByAKbtpRirAVLxOGKzG%2BYMOmsLRBPujZep%2Fca%2B1%2FXzW3xX56VXkh5w6ItYhIctEFifQQhw3rTmvoljyHw%3D%3D--4pK6xBEgChjqgmte--LH4Q1qSnhU%2FYX9JgTzGuSQ%3D%3D;'
    }
    print('..ok')
    yield scrapy.Request(url, method="POST", headers=headers, body=payload, callback=self.parse_project)

Returns:

2022-02-23 07:06:55 [scrapy.core.engine] DEBUG: Crawled (403) <POST https://www.kickstarter.com/graph> (referer: None)
2022-02-23 07:06:55 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <403 https://www.kickstarter.com/graph>: HTTP status code is not handled or not allowed

Python 请求中的代码(有效):

import requests
import json

url = "https://www.kickstarter.com/graph"

payload = json.dumps([
  {
    "operationName": "Campaign",
    "variables": {
      "slug": "leightonconnor/akashic-titan-blue-bolt"
    },
    "query": "query Campaign($slug: String!) {\n  project(slug: $slug) {\n    id\n    isSharingProjectBudget\n    risks\n    story(assetWidth: 680)\n    currency\n    spreadsheet {\n      displayMode\n      public\n      url\n      data {\n        name\n        value\n        phase\n        rowNum\n        __typename\n      }\n      dataLastUpdatedAt\n      __typename\n    }\n    environmentalCommitments {\n      id\n      commitmentCategory\n      description\n      __typename\n    }\n    __typename\n  }\n}\n"
  }
])
headers = {
  'content-type': 'application/json',
  'x-csrf-token': 'AZsT67Z9s-LHZt6ZJXLSQWJlNdd7biKz2XDfFMkcYMZrNufH1OWoFhNBlXIvxCrxKRzV6l8bG_Z6QlcRoYMe_g',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
  'cookie': '_ksr_session=fc2U7qXXaRN91foNiE53NyU3s181NZO0Ll57xPkYxZ5iyUNgus35a0HwsPBTfViBY%2ByAKbtpRirAVLxOGKzG%2BYMOmsLRBPujZep%2Fca%2B1%2FXzW3xX56VXkh5w6ItYhIctEFifQQhw3rTmvoljyHw%3D%3D--4pK6xBEgChjqgmte--LH4Q1qSnhU%2FYX9JgTzGuSQ%3D%3D;'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.status_code)
print(response.json()[0]['data']['project']['risks'])

以下是它对我的作用:

  1. 打开您要抓取的页面。
  2. 查看检查工具中的网络选项卡。
  3. 找到包含所需信息的 GraphQl 请求。
  4. 右键单击它并转到复制 > 复制为卷曲 (bash)。 (这是假设您使用的是 chrome,我认为其他浏览器也有,但我使用 chrome)。
  5. 转到 curl2scrapy 并粘贴您的 curl 命令。它会给你 headers 和 payload.
  6. 在您 运行 之前,它将查询中的所有 \n 替换为 \n