初学爬虫小程序
为什么用json()输出不行
用text可以,
应该怎么解决 可以用resp.content.decode() json格式和xml格式都能返回 图片音乐等二进制格式就用resp.content
text有时候还会牵扯到编解码的问题 百度翻译挺难的,建议有道尝试一下 json()的前提是网站返回的是json的数据,从text看出,它返回的是html,所以用json加载报错,说第1行第1列的字符异常。
你的找个返回json格式的网页试试。 这和你访问的网站有关,html是text格式,网站有.json是json暑假 学习终。。。。。 求助 {:5_90:} 本帖最后由 临时号 于 2022-9-3 12:56 编辑
import re
import requests
def n(r, o):
t = 0
while t < len(o) - 2:
a = o
if a >= "a":
a = ord(a) - 87
else:
a = int(a)
if "+" == o:
a = r >> a
else:
a = r << a
if "+" == o:
r = r + a & 4294967295
else:
r = r ^ a
t += 3
return r
def a(r):
if type(r) == list:
o = 0
t = list(len(r))
while o < len(r):
t = r
o += 1
return t
return r
def e(r):
global i
o = re.findall("[\uD800-\uDBFF][\uDC00-\uDFFF]",r)
if o == []:
t = len(r)
if t > 30:
r = r + r + r[-10:]
else:
e = re.split("[\uD800-\uDBFF][\uDC00-\uDFFF]",r)
C = 0
h = len(e)
f = []
while h > C:
"" != e and f.extend(a(e.split("")))
C != h - 1 and f.append(o)
C += 1
g = len(f)
if g > 30:
r = "".join(f) + "".join(f) + "".join(f[-10:])
u = None
l = chr(103) + chr(116) + chr(107)
if None != i:
u = i
else:
i = "320305.131321201" or ""
u = i or ""
d = u.split(".")
m = int(d) or 0
s = int(d) or 0
S =
c = 0
v = 0
while v < len(r):
A = ord(r)
if 128 > A:
S = A
c += 1
else:
if 2048 > A:
S = A >> 6 | 192
c += 1
else:
if 55296 == (64512 & A) and v + 1 < len(r) and 56320 == (64512 & ord(r)):
v += 1
A = 65536 + ((1023 & A) << 10) + (1023 & ord(r))
S = A >> 18 | 240
c += 1
S = A >> 12 & 63 | 128
c += 1
else:
S =A >> 12 | 224
c += 1
S = A >> 6 & 63 | 128
c += 1
S = 63 & A | 128
c += 1
v += 1
for count in range(S.count(0)):
S.remove(0)
p = m
F = chr(43) + chr(45) + chr(97) + (chr(94) + chr(43) + chr(54))
D = chr(43) + chr(45) + chr(51) + (chr(94) + chr(43) + chr(98)) + (chr(43) + chr(45) + chr(102))
b = 0
while b < len(S):
p += S
p = n(p,F)
b += 1
p = n(p,D)
p ^= s
if 0 > p:
p = (2147483647 & p) + 2147483648
p %= 1e6
return str(int(p)) + "." + str(int(p) ^ m)
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
"Cookie":"BIDUPSID=A92A6802F7D86AED4D4E206949C7BDAD; PSTM=1650383456; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=lKbG5Van54QlllVVlRfkZtSHB5UVFyZDJRQTFYVkZiRDBvMVk4Znc4OUpwWTlpRVFBQUFBJCQAAAAAAAAAAAEAAAChTKa30-7W5tL40NAzNDUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEkYaGJJGGhiUU; BDUSS_BFESS=lKbG5Van54QlllVVlRfkZtSHB5UVFyZDJRQTFYVkZiRDBvMVk4Znc4OUpwWTlpRVFBQUFBJCQAAAAAAAAAAAEAAAChTKa30-7W5tL40NAzNDUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEkYaGJJGGhiUU; APPGUIDE_10_0_2=1; BAIDUID=78B7B37E0E97AF8B823966378D315602:FG=1; newlogin=1; H_PS_PSSID=; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1660558471,1660720878,1660799867,1660886179; ab_sr=1.0.1_MDEwYjlkYzllNDcwNDU0Nzk3NjVhNzAzNmMyZTRhNTNjNmE0Y2ZlNjJkZWZmODc2OWM5ZjQ2NzQxMDk3MTU1MjkxN2Y5MzdiYjYwNDRlOWNmYTYyZTcxZTdiZjAwOGMxZGFiOWM2NmFhYjMzMjQxNWE0NTVlODY2ZDg5MTgzZjVkYzVlYmI5MDA0MmUyNTNjMDBjNWQ3Zjg5NzFkM2IzMzk0Y2ZiNGQxNjRkYzBhYzdhNDY3MTU3ZGUzMzMxNTc3; BAIDUID_BFESS=78B7B37E0E97AF8B823966378D315602:FG=1; delPer=0; PSINO=6; BA_HECTOR=8425858la08l20agak8i397n1hfum9i17; ZFY=x65kMuW:BavVpgIHCMW7HIirB1oQin3GXivMNQ6Y96Hc:C; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1660904815"
}
word = input("请输入待翻译的内容:")
i = "320305.131321201"
data = {
"from":"en",
"to":"zh",
"query":word,
"transtype":"realtime",
"simple_means_flag":"3",
"sign":e(word),
"token":"c4882f7699e4c8b6fce27da420eff7f2",
"domain":"common"
}
response = requests.post("https://fanyi.baidu.com/v2transapi?from=en&to=zh",headers=headers,data=data)
if response.status_code == 200:
response.encoding = "utf-8"
print(response.json()["trans_result"]["data"]["dst"])
页:
[1]