刚开始学爬虫,尝试爬一下江西药监局做练习,如下是代码:
import json
import requests
if __name__ == '__main__':
url = 'http://zhjg.mpa.jiangxi.gov.cn/yjlic-search-svr/search/list'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
}
data = {
'pageNumber': '1',
'pageSize': '10',
'lictypeid': "CO_PRO_XK",
'status': "1"
}
page_json = requests.post(url = url, headers = headers,data=data).json()
with open('./yaojian.json','w',encoding='utf-8') as fp:
json.dump(page_json,fp,ensure_ascii=False)
print("over!!!")
但结果是:{"code": 500, "msg": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", "data": null},不知道如何解决
【以下回答由 GPT 生成】
这个问题是因为客户端请求中的Content-Type被服务器拒绝了。根据错误信息,服务器不支持Content-Type为'application/x-www-form-urlencoded;charset=UTF-8'。要解决这个问题,可以尝试以下几种方法:
requests
库发送POST请求时,可以将请求数据通过json
参数传递,而不是通过data
参数传递。修改代码如下:import json
import requests
if __name__ == '__main__':
url = 'http://zhjg.mpa.jiangxi.gov.cn/yjlic-search-svr/search/list'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76',
"Content-Type": "application/json"
}
data = {
'pageNumber': '1',
'pageSize': '10',
'lictypeid': "CO_PRO_XK",
'status': "1"
}
page_json = requests.post(url=url, headers=headers, json=data).json()
with open('./yaojian.json', 'w', encoding='utf-8') as fp:
json.dump(page_json, fp, ensure_ascii=False)
print("爬取完成!")
在修改后的代码中,将headers
中的Content-Type
设置为application/json
,并且使用json
参数传递请求数据。
Content-Type
为其他类型,例如application/x-www-form-urlencoded
。修改代码如下:import json
import requests
if __name__ == '__main__':
url = 'http://zhjg.mpa.jiangxi.gov.cn/yjlic-search-svr/search/list'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76',
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
'pageNumber': '1',
'pageSize': '10',
'lictypeid': "CO_PRO_XK",
'status': "1"
}
page_json = requests.post(url=url, headers=headers, data=data).json()
with open('./yaojian.json', 'w', encoding='utf-8') as fp:
json.dump(page_json, fp, ensure_ascii=False)
print("爬取完成!")
在修改后的代码中,将headers
中的Content-Type
设置为application/x-www-form-urlencoded
,并保持data
参数不变。
Content-Type
设置为application/x-www-form-urlencoded
。修改代码如下:import json
import requests
if __name__ == '__main__':
url = 'http://zhjg.mpa.jiangxi.gov.cn/yjlic-search-svr/search/list'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76',
"Content-Type": "application/x-www-form-urlencoded"
}
data = 'pageNumber=1&pageSize=10&lictypeid=CO_PRO_XK&status=1'
page_json = requests.post(url=url, headers=headers, data=data.encode('utf-8')).json()
with open('./yaojian.json', 'w', encoding='utf-8') as fp:
json.dump(page_json, fp, ensure_ascii=False)
print("爬取完成!")
在修改后的代码中,将data
参数修改为以字符串形式表示的表单数据,并将其编码为utf-8格式。