Python in use error (continuous update ...)

Source: Internet
Author: User

1. When sending a request using requests, the response data is turned into JSON hint, there is no JSON to decode

Workaround:

This is due to the sending of the requested data, there is an error, the response error such as 404 400, so can not find the JSON to decode

Examples are as follows:

" " {' userName ': ' 13718651996 ',            ' passwd ': ' yhlxxxx870120 ',            ' validatecode ': ' abc ',            ' rememberme ': ' True ' }"'https://secure.elong.com/passport/ajax/elongLogin'   = Session (). Post (url,data=postdata)print(Response.json ())

In fact, it is clear from the diagram that the problem is apparent because postdata is a string type, not a dict dictionary

such as code if the following error is reported:

Workaround, the eval () function converts the string into a dictionary, as shown below

" " {' userName ': ' 13718651996 ',            ' passwd ': ' yhlxxxx870120 ',            ' validatecode ': ' abc ',            ' rememberme ': ' True ' }"'https://secure.elong.com/passport/ajax/elongLogin'   = Session (). Post (url,data=eval (postdata))print(Request.json ())

Some might say, aren't you superfluous? The postdata is directly defined as a dictionary; you're right, you can do that.

PostData = {'UserName':'13718651996',            'passwd':'yhlxxxx870120',            'Validatecode':'ABC',            'RememberMe':'true'}url='Https://secure.elong.com/passport/ajax/elongLogin'Request= Session (). Post (url,data=postdata)Print(Request.json ())

But if the following string of data, stored in the database, or Excel, read out the default is the string, if you read it directly using the error will appear in this article,

So what's wrong with this is to know that this is a small detail; debug output, the discovery output is the same as the following,

There is nothing wrong with this is the dictionary {' xxxx ': ' xxx ' ...}, but it is a string. Easy to ignore, so use the eval (postdata) function to turn into a dictionary

{'UserName':'13718651996',            'passwd':'yhlxxxx870120',            'Validatecode':'ABC',            'RememberMe':'true'}

Correct code:

PostData = "' {'UserName':'13718651996',            'passwd':'yhlxxxx870120',            'Validatecode':'ABC',            'RememberMe':'true'} ' URL='Https://secure.elong.com/passport/ajax/elongLogin'Request= Session (). Post (url,data= eval (postdata))Print(Request.json ())

------------------------------I'm a gorgeous divider line----------------------------------

2.

Python in use error (continuous update ...)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.