JSearch

免费增值
Verified
通过 OpenWeb Ninja | 已更新 vor 5 Tagen | Data
人气

9.9 / 10

延迟

1,672ms

服务等级

100%

Health Check

100%

返回所有讨论

Key Error trying to get to "data" with Python

Rapid account: Sara M 2261
SaraM2261
vor 12 Tagen

Hello!
I can retrieve the job listings, and I can take a user input for the query string to filter just the jobs with that keyword. However, trying to use the data key to display items on the front side, I am getting a key error every time I try to get to the data key (where all the jobs are stored as items). (I am using Python)

querystring = {“query”:{jobSearchInput},“page”:“1”,“num_pages”:“1”,“date_posted”:“week”,“employment_types”:“FULLTIME”}

response = requests.get(url, headers=headers, params=querystring)

responseData = response.json()

appData = responseData[‘data’]

^ the last line should work but throws a key error saying the data key does not exist. I am printing the parsed response prior to this and I can see the data key in it.

The data is there - on the front-end page I am printing it whole successfully without being able to access the “data” key.

Thank you for any help!

Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented vor 11 Tagen

Nice, great to hear the issue is resolved!

Please don’t hesitate to reach out in case you have any questions or if anything is needed.

Rapid account: Sara M 2261
SaraM2261 Commented vor 11 Tagen

Good morning!
I was able to get it working with a different method. Here’s what I used instead of response.json()

response = requests.get(url, headers=headers, params=querystring)

    jobsData = response.text
    parsedJobsData = json.loads(jobsData) #this was the winner :)
    readyData = parsedJobsData['data'] #then this worked
Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented vor 12 Tagen

What does print(responseData.keys()) prints out?

Rapid account: Sara M 2261
SaraM2261 Commented vor 12 Tagen

the value of responseData is that parsed JSON object. Here’s the start of it after a user search:

{‘status’: ‘OK’, ‘request_id’: ‘545299bb-7101-4421-b9af-cc980f84cdc2’, ‘parameters’: {‘query’: ‘banana’, ‘page’: 1, ‘num_pages’: 1, ‘date_posted’: ‘week’, ‘employment_types’: [‘FULLTIME’]}, ‘data’: [{‘job_id’: ‘BJFHKQKF3QSc4P48AAAAAA==’, ‘employer_name’: ‘Banana Republic’, ‘employer_logo’: ‘https://bananarepublic.gap.com/Asset_Archive/BRWeb/Assets/Marketing/BR_Logo_R1.jpg’, ‘employer_website’: ‘http://www.gapinc.com’, ‘employer_company_type’: ‘Retail’, ‘job_publisher’: ‘Gap Inc.’, ‘job_employment_type’: ‘FULLTIME’, ‘job_title’: ‘Lead, Full Time - Somerset Collection’, ‘job_apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘job_apply_is_direct’: False, ‘job_apply_quality_score’: 0.6787, ‘apply_options’: [{‘publisher’: ‘Gap Inc.’, ‘apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘is_direct’: False}, {‘publisher’: ‘LinkedIn’, ‘apply_link’:

Rapid account: Sara M 2261
SaraM2261 Commented vor 12 Tagen

responseData is a variable that holds that parsed JSON response from the API.
print(type(responseData)) is checking the type to make sure it was the correct type of object and didn’t need further parsing before using the
appData = responseData[‘data’] to get to the data key and the items inside it for displaying.)

The output is <class ‘dict’> stating that it is a Python dictionary and so that responseData[‘data’] should work to access the data key items.

Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented vor 12 Tagen

What is the value of responseData after doing responseData = response.json()?

Also, what does print(type(responseData)) outputs?

Rapid account: Sara M 2261
SaraM2261 Commented vor 12 Tagen

Here is the start of the JSON object that I am trying to get to the key 'data’
As the user I entered “banana” for the search query - you can see it is a string below. And also that ‘data’ key is present.

Live Results
{‘status’: ‘OK’, ‘request_id’: ‘545299bb-7101-4421-b9af-cc980f84cdc2’, ‘parameters’: {‘query’: ‘banana’, ‘page’: 1, ‘num_pages’: 1, ‘date_posted’: ‘week’, ‘employment_types’: [‘FULLTIME’]}, ‘data’: [{‘job_id’: ‘BJFHKQKF3QSc4P48AAAAAA==’, ‘employer_name’: ‘Banana Republic’, ‘employer_logo’: ‘https://bananarepublic.gap.com/Asset_Archive/BRWeb/Assets/Marketing/BR_Logo_R1.jpg’, ‘employer_website’: ‘http://www.gapinc.com’, ‘employer_company_type’: ‘Retail’, ‘job_publisher’: ‘Gap Inc.’, ‘job_employment_type’: ‘FULLTIME’, ‘job_title’: ‘Lead, Full Time - Somerset Collection’, ‘job_apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘job_apply_is_direct’: False, ‘job_apply_quality_score’: 0.6787, ‘apply_options’: [{‘publisher’: ‘Gap Inc.’, ‘apply_link’: ‘https://www.gapinc.com/en-us/jobs/w44/87/lead,-full-time-somerset-collection’, ‘is_direct’: False}, {‘publisher’: ‘LinkedIn’, ‘apply_link’:

Rapid account: Sara M 2261
SaraM2261 Commented vor 12 Tagen

That is actually a variable that takes a user input. I can send an image of the object that is returned that I am working with. That variable is a string in the object after it runs.

Rapid account: Letscrape 6 B R Ba 3 Qgu O 5
letscrape-6bRBa3QguO5 Commented vor 12 Tagen

Hi there,

Thanks for your message.

Try to replace “{jobSearchInput}” with “jobSearchInput” (in querystring query), resulting in: querystring = {“query”:jobSearchInput,“page”:“1”,“num_pages”:“1”,“date_posted”:“week”,“employment_types”:“FULLTIME”}

It seems like a set is passed to the “query” instead of a string.

Does it work for you?

Thank you,
Adam @ OpenWeb Ninja

加入讨论 - 在下面添加评论:

登录/注册以发布新的评论