#响应的数据应该不是json, 报错:requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
代码如下:
import requests
import json
class zijietiaodong:
def __init__(self):
self.headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}
self.url='https://jobs.bytedance.com/api/v1/search/job/posts?keyword=python&limit=10&offset=0&job_category_id_list=&tag_id_list=&location_code_list=&subject_id_list=&recruitment_id_list=&portal_type=2&job_function_id_list=&portal_entrance=1&_signature=v95cqQAAAADdDLpRExjZ-L.eXLAANtV'
self.paremas={ 'keyword':'python',
'limit':'10',
'offset':'10',
'job_category_id_list':'',
'tag_id_list':'',
'location_code_list':'',
'subject_id_list':'',
'recruitment_id_list':'',
'portal_type':'2',
'job_function_id_list':'',
'portal_entrance':'1',
'_signature': 'v95cqQAAAADdDLpRExjZ-L.eXLAANtV',
'Accept-Encoding':'gzip, deflate, br',
'Referer':'https://jobs.bytedance.com/experienced/position?keywords=python',
'Sec-Ch-Ua':'"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'X-Csrf-Token':'77_XAVdEXt3YWK3Oe7MW5Xhr3Icg9DtAFS1pqp6mSvk=',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN',
'Connection': 'keep-alive',
'Content-Length': '217',
'Content-Type': 'application/json',
'Cookie': 'locale=zh-CN; channel=office; platform=pc; device-id=7248249605207737913; s_v_web_id=verify_lja26fck_oENhLzcw_lDE0_415D_Ayjy_57A2pO2X8NuY; atsx-csrf-token=77_XAVdEXt3YWK3Oe7MW5Xhr3Icg9DtAFS1pqp6mSvk%3D',
'Env': 'undefined',
'Host': 'jobs.bytedance.com',
'Origin:https': '//jobs.bytedance.com',
'Portal-Channel': 'office',
'Portal-Platform': 'pc',
'Referer:https': '//jobs.bytedance.com/experienced/position?keywords=python',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Website-Path': 'society',
}
def get_html(self):
# for i in range(20):
# paremas=self.paremas
html=requests.post(headers=self.headers,url=self.url,data=self.paremas).json()
# html=html.json()
print(html)
item={}
for job in html['data']['job_post_list']:
print(job)
def run(self):
self.get_html()
if __name__=='__main__':
spider=zijietiaodong()
spider.run()
#请求帮忙
从代码中可以看出,你正在尝试使用requests.post
方法发送一个POST请求到指定的URL,并期望返回JSON格式的数据。但是,根据你提供的错误信息:requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
,这意味着服务器返回的数据并不是有效的JSON格式,而是其他格式的数据。
为了解决这个问题,你可以先检查服务器返回的数据类型和内容,确保它确实是JSON格式的。你可以使用response.headers['Content-Type']
来查看响应头中的数据类型,如果不是application/json
,那么可能是其他格式的数据。此外,你还可以使用response.text
来查看响应的具体内容。
如果你确定服务器返回的是JSON格式的数据,但仍然遇到这个错误,那么可能是因为服务器返回的数据不完整或者存在问题。你可以尝试打印出完整的响应内容,以便进一步分析问题所在。
另外,你的代码中有一个小错误,self.paremas={ 'keyword':'python', ...}
这一行应该缩进,与def __init__(self):
对齐。以下是修改后的代码:
import requests
import json
class zijietiaodong:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}
self.url = 'https://jobs.bytedance.com/api/v1/search/job/posts?keyword=python&limit=10&offset=0&job_category_id_list=&tag_id_list=&location_code_list=&subject_id_list=&recruitment_id_list=&portal_type=2&job_function_id_list=&portal_entrance=1&_signature=v95cqQAAAADdDLpRExjZ-L.eXLAANtV'
self.paremas = {'keyword': 'python',
'limit': '10',
'offset': '10',
'job_category_id_list': '',
'tag_id_list': '',
'location_code_list': '',
'subject_id_list': '',
'recruitment_id_list': '',
'portal_type': '2',
'job_function_id_list': '',
'portal_entrance': '1',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://jobs.bytedance.com/experienced/position?keywords=python',
'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'X-Csrf-Token': '77_XAVdEXt3YWK3Oe7MW5Xhr3Icg9DtAFS1pqp6mSvk=',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN',
'Connection': 'keep-alive',
'Content-Length': '217',
'Content-Type': 'application/json',
'Cookie': 'locale=zh-CN; channel=office; platform=pc; device-id=7248249605207737913; s_v_web_id=verify_lja26fck_oENhLzcw_lDE0_415D_Ayjy_57A2pO2X8NuY; atsx-csrf-token=77_XAVdEXt3YWK3Oe7MW5Xhr3Icg9DtAFS1pqp6mSvk%3D',
'Env': 'undefined',
'Host': 'jobs.bytedance.com',
'Origin:https://jobs.bytedance.com//jobs.bytedance.com/experienced/position?keywords=python',
'Portal-Channel': 'office',
'Portal-Platform': 'pc',
'Referer:https://jobs.bytedance.com//experienced/position?keywords=python',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Website-Path': '/society'}
def get_html(self):
html = requests.post(headers=self.headers, url=self.url, data=self.paremas).json()
print(html)
item = {}
for job in html['data']['job_post_list']:
print(job)
def run(self):
self.get_html()
if __name__ == '__main__':
spider = zijietiaodong()
spider.run()
print(requests.post(headers=self.headers,url=self.url,data=self.paremas))
看下输出的是什么(不要后面的json()解析)
这个问题的本质是无法解析响应中的非法JSON格式数据。需要通过以下几步解决:
import requests
import json
response = requests.get(url)
try:
data = json.loads(response.text)
except json.JSONDecodeError:
print("JSON decode error!")
# 其他处理代码
import requests
response = requests.get(url)
if response.status_code == 200:
try:
data = response.json()
except json.JSONDecodeError:
print("Response is not valid JSON format!")
data = response.text
总之,在使用requests库发送请求时,需要对响应进行合理的处理,避免出现JSONDecodeError异常导致程序崩溃。需要根据实际情况进行具体的处理方案。
为什么一样的错误