About the python3 crawler post json cannot crawl data?

problem description

python version 3.6, according to the online template to write the code, found that it can not be crawled. I don"t know what"s wrong. Is there a big god to come out and give us some advice?

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

import urllib.request
import urllib.parse
import json
url = r"XXXXXXX"
headers = {"Accept":"application/json, text/javascript, */*; q=0.01",
    "Accept-Language":"zh-CN,zh;q=0.8",
    "Content-Length":"2",
    "Content-Type":"application/json",
    "Cookie":"XXXXX",
    "Host":"XXXX",
    "Origin":"XXXXX",
    "Proxy-Connection":"keep-alive",
    "Referer":"XXXXXX",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like             Gecko) Chrome/55.0.2883.87 Safari/537.36"
    }
data = {"requestType":"3",
    "quoteBeginTime":"2018-07-12",
    "quoteEndTime":"2018-07-13",
    "supplierSign":"false",
    "newQuality":"false",
    "newAutoQuote":"false",
    "inquiryTimesDesc":"false",
    "page":"1",
    "pageSize":"20",
    "orderBy":"0",
    "ordered":"false"
    }
data=json.dumps(data)
data=data.encode()
wed_content=urllib.request.Request(url,headers=headers,data=data)
wed_content=urllib.request.urlopen(wed_content,timeout=120)
wed_content=wed_content.read()
wed_content=wed_content.decode("utf-8")
print(wed_content)



what result do you expect? What is the error message actually seen?


urllib.error.HTTPError: HTTP Error 400: Bad Request
JSON
:Traceback (most recent call last):
  File "E:/pg/batulu/4.py", line 31, in <module>
    wed_content=urllib.request.urlopen(wed_content,timeout=120)
  File "C:\Users\hasee\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\hasee\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\hasee\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
    "http", request, response, code, msg, hdrs)
  File "C:\Users\hasee\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\hasee\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\hasee\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 400: Bad Request

Mar.28,2021

are you sure the JSON data is full of string types? Try going back to the original type:

data = {'requestType':'3',
    'quoteBeginTime':'2018-07-12',
    'quoteEndTime':'2018-07-13',
    'supplierSign': False,
    'newQuality': False,
    'newAutoQuote': False,
    'inquiryTimesDesc': False,
    'page': 1,
    'pageSize': 20,
    'orderBy': 0,
    'ordered': False
    }
Menu