很簡單的腳本,功能也簡單,在一臺Win10和一臺Win7運行都正常,放到另一臺新裝的win7就報錯。百度了好久也不知道原因。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
url = 'https://someweb.com/submit'
headers={
"Host": "something.com",
"Connection": "keep-alive",
"Content-Length": "100",
}
def format(Cookie):
cookies = {}
for line in Cookie.split(";"):
if line.find("=") != -1:
name, value = line.strip().split("=")
cookies[name] = value
return cookies
fp = open ("1.txt","r")
Cookie = fp.read()
fp.close()
CookieNew = format(Cookie)
DataNew = '{"vId":"000104","serviceId":"1002"}'
requests.post(url, data=https://bbs.csdn.net/topics/DataNew, headers=headers, cookies=CookieNew)
format函式就是把cookie轉換成JSON。
直接copy過去運行就報這個錯:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position xx-xx:
ordinal not in range(256)
百度之后,把打開檔案加上encoding 改成 fp = open ("1.txt","r",encoding = "utf-8-sig"),報錯就是:
{"timestamp":"2020-04-23 16:47:15","status":500,"error":"Internal
Server Error","exception":"org.springframework.http.converter.HttpMessageNotRead
ableException","message":"Could not read document: Unexpected character ('i' (co
de 105)): was expecting comma to separate OBJECT entries\n at [Source: java.io.P
ushbackInputStream@6bc0aae7; line: 1, column: 105]; nested exception is com.fast
erxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): wa
s expecting comma to separate OBJECT entries\n at [Source: java.io.PushbackInput
Stream@6bc0aae7; line: 1, column: 105]","path":"/dir/cou/submit"}這是網站回傳的錯,但是搞不明白為啥,就在requests.post那句上。
問題是這個py腳本在那兩臺win10和win7上運行一點問題沒有啊,啥都不用改。。是新裝的這個win7缺什么環境了嗎。
cookie內容是:
Hm_lvt_4a693f348daf8481dd541c844122c053=1585791910; JSESSIONID=D524881BB65001A53B445AA4EDC14FC7; Hm_lpvt_4a693f348daf8481dd541c844122c053=1587654145; Hm_lvt_bc864c0a0574a7cabe6b36d53206fb69=1583560881;
無從下手的感覺,就覺得是編碼問題,但又不知道咋改。

uj5u.com熱心網友回復:
UnicodeEncodeError 的解決了,就是編碼問題,改成utf-8就不報錯了。 現在就剩網站報的那個錯誤了。沒法查怎么回事,百度有說是JSON問題的,可是那兩臺正常的也是用的這個檔案呢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/54185.html
