【问题标题】:How to retrieve data from json response with scrapy?如何使用scrapy从json响应中检索数据?
【发布时间】:2019-04-17 02:52:46
【问题描述】:

我在 python 中使用 scrapy。

这是我的网址:

https://www.workingnomads.co/jobsapi/job/_search?sort=expired:asc,premium:desc,pub_date:desc&_source=company,category_name,description,location_base,instructions,id,external_id,slug,title,pub_date,tags,source,apply_url,premium,expired,use_at

我的代码:

def parse(self, response):
        jsonresponse = json.loads(response.body_as_unicode())
        print("============================================================================================================================")
        print(jsonresponse["hits"]["hits"])

它以 JSON 格式返回响应,如下所示。

如何获取特定键的值?

这是邮递员的回复

我想检索apply_url 键值。

【问题讨论】:

  • 您要检索哪些键?你到底在哪里挣扎?
  • 请查看我编辑的问题@petezurich

标签: python json scrapy


【解决方案1】:

你会想要访问:

['hits']['hits'][x]['_source']['apply_url']

其中 x 是 hits 下的项目/节点数。见https://jsoneditoronline.org/#left=cloud.22e871cf105e40a5ba32408f6aa5afeb&right=cloud.e1f56c3bd6824a3692bf3c80285ae727

如您所见,在 hits -> hits 下有 10 个项目或节点。 apply_url 位于 _source 下方,用于每个项目。

def parse(self, response):
    jsonresponse = json.loads(response.body_as_unicode())
    print("============================================================================================================================")
    for x, node in enumerate(jsonresponse):
        print(jsonresponse['hits']['hits'][x]['_source']['apply_url'])

例如,print(jsonresponse['hits']['hits'][0]['_source']['apply_url']) 会产生:

https://boards.greenhouse.io/mesosphere/jobs/1422922?gh_jid=1422922

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 2020-06-18
    • 1970-01-01
    • 2022-01-22
    • 2023-03-15
    • 2017-08-10
    • 2020-03-12
    • 1970-01-01
    • 2014-01-03
    相关资源
    最近更新 更多