nahongyan1997 发表于 2021-8-11 08:58
我帮你把代码改好了呦,记得设置最佳答案呦。
Traceback (most recent call last):
File "C:/Users/中维电器维修/Desktop/找来的爬虫.py", line 43, in <module>
html = GzipFile(fileobj=BytesIO(response.read())).read()
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 276, in read
return self._buffer.read(size)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 463, in read
if not self._read_gzip_header():
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 411, in _read_gzip_header
raise OSError('Not a gzipped file (%r)' % magic)
OSError: Not a gzipped file (b'{"')
似乎不行
wangka 发表于 2021-8-10 21:03
我问一下,网站有没有问题
搜狗翻译 网址https://fanyi.sogou.com/text
wangka 发表于 2021-8-10 21:07
你的逗号!,,看看呢?
爬虫是不断更新的
逗号我自己打的,应该没问题
nahongyan1997 发表于 2021-8-11 08:58
我帮你把代码改好了呦,记得设置最佳答案呦。
Traceback (most recent call last):
File "C:/Users/中维电器维修/Desktop/找来的爬虫.py", line 43, in <module>
html = GzipFile(fileobj=BytesIO(response.read())).read()
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 276, in read
return self._buffer.read(size)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 463, in read
if not self._read_gzip_header():
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 411, in _read_gzip_header
raise OSError('Not a gzipped file (%r)' % magic)
OSError: Not a gzipped file (b'{"')
一言难尽
emm
nahongyan1997 发表于 2021-8-11 08:58
我帮你把代码改好了呦,记得设置最佳答案呦。
Traceback (most recent call last):
File "C:/Users/中维电器维修/Desktop/找来的爬虫.py", line 43, in <module>
html = GzipFile(fileobj=BytesIO(response.read())).read()
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 276, in read
return self._buffer.read(size)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 463, in read
if not self._read_gzip_header():
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\gzip.py", line 411, in _read_gzip_header
raise OSError('Not a gzipped file (%r)' % magic)
OSError: Not a gzipped file (b'{"')
一言难尽
磊之茶 发表于 2021-8-11 15:59
emm
你没动headers吧,
这个报警的意思是 文件不是gzip文件,
你把
html = GzipFile(fileobj=BytesIO(response.read())).read()
改成
try:
html = GzipFile(fileobj=BytesIO(response.read())).read()
except:
html = response.read().decode()
哦呵呵呵呵呵呵
nahongyan1997 发表于 2021-8-11 16:02
你没动headers吧,
这个报警的意思是 文件不是gzip文件,
你把
谢谢,不过
Traceback (most recent call last):
File "C:/Users/中维电器维修/Desktop/找来的爬虫.py", line 47, in <module>
result = j.loads(html)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
磊之茶 发表于 2021-8-11 16:04
谢谢,不过
说实话要不你用 python3.8吧,3.8没这些个毛病。
你这是得慢慢调试问题了,
本帖最后由 磊之茶 于 2021-8-11 16:37 编辑
nahongyan1997 发表于 2021-8-11 16:10
说实话要不你用 python3.8吧,3.8没这些个毛病。
你这是得慢慢调试问题了,
重装了,但还是报错
Traceback (most recent call last):
File "C:\Users\中维电器维修\Desktop\找来的爬虫.py", line 47, in <module>
result = j.loads(html)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
磊之茶 发表于 2021-8-11 16:17
呃,怎么升级
先把3.7卸载干净,然后上官网下载个3.8安装包,安上就完事了
有鱼币否
没有再试一次
nahongyan1997 发表于 2021-8-11 16:19
先把3.7卸载干净,然后上官网下载个3.8安装包,安上就完事了
重装了,但还是报错
Traceback (most recent call last):
File "C:\Users\中维电器维修\Desktop\找来的爬虫.py", line 47, in <module>
result = j.loads(html)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\中维电器维修\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
你在
result = j.loads(html)
前面加一句
print(html)
看看打印出了什么
nahongyan1997 发表于 2021-8-11 16:54
你在
前面加一句
啥都没有‘’
html 是空的
磊之茶 发表于 2021-8-11 16:58
啥都没有
html 是空的
把你现在的代码完整的复制发上来,肯定跟我发的不一样。
我还真不信这个斜了
nahongyan1997 发表于 2021-8-11 17:02
把你现在的代码完整的复制发上来,肯定跟我发的不一样。
我还真不信这个斜了
from urllib.request import *
from urllib.parse import urlencode
import json as j
from pprint import pprint
from gzip import GzipFile
from io import BytesIO
header = {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,ko;q=0.8,ga;q=0.7",
"Connection": "keep-alive",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"Host": "fanyi.sogou.com",
"Origin": "https://fanyi.sogou.com",
"Referer": "https://fanyi.sogou.com/text",
"Content-Type": "application/json;charset=UTF-8",
"Cookie": "ABTEST=0|1628641545|v17; IPLOC=CN2102; SUID=1C377177EF53A00A0000000061131909; SNUID=AF83C5C3B3B67D8E1C7E84C5B48B72CD; FUV=7780e23e4a0de1b60632bc7d414ef4d0; SGINPUT_UPSCREEN=1628641551176; SUV=1628641551193"
}
data = {
'client': "pc",
'exchange':'false',
'fr':"browser_pc",
'from':"auto",
'needQc':'1',
's':"d78cdd5a1a76770c398ca51493d3a2c0",
'text':"I like FishC.com",
'to':"zh-CHS",
'uuid':"12681881-4857-4a6a-be0e-8c7bd28a73d3"
}
data = j.dumps(data).encode('utf-8')
header["Content-Length"] = len(data)
ps=ProxyHandler({'http':'223.244.179.85:3256'})
opener = build_opener(ps)
Req = Request(r'https://fanyi.sogou.com/api/transpc/text/result',data,header)
response = opener.open(Req)
try:
html = GzipFile(fileobj=BytesIO(response.read())).read()
print(html)
except:
html = response.read().decode()
print(html)
print(html)
result = j.loads(html)
pprint(result)
本帖最后由 nahongyan1997 于 2021-8-11 17:15 编辑
给你看看我这打印出来的东西,我什么都没动
Python 3.8.8 (C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe)
>>> %Run 1.py
b'{"status":0,"info":"success","zly":"zly","node":"1","data":{"translate":{"zly":"zly","errorCode":"0","qc_type":"2","index":"content0","from":"en","source":"sogou","text":"I like FishC.com","to":"zh-CHS","id":"a5ffe05c-34dd-4467-8308-9cb786a9a93d","dit":"\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2FishC.com","orig_text":"I like FishC.com","diff_text":"\xee\x90\x8aI like FishC.com\xee\x90\x8b\xee\x90\x8cilikefishc.com\xee\x90\x8d","qc_text":"ilikefishc.com","md5":""},"detect":{"zly":"zly","detect":"en","errorCode":"0","language":"\xe8\x8b\xb1\xe8\xaf\xad","id":"573c5190-fa84-11eb-913f-31d41187ce12","text":"I like FishC.com"},"sgtkn":"CB157737AF83C5C3B3B67D8E1C7E84C5B48B72CD611394A3","wordCard":{"title":false,"show":false,"usual_Dict":"","second_query":"","exchange":"","levelList":""},"detail":"","book":"","network":"","pic":"","voice":"","keywords":[{"key":"like","value":"\xe5\x83\x8f\xef\xbc\x9b\xe7\x9b\xb8\xe4\xbc\xbc\xef\xbc\x9b\xe7\xb1\xbb\xe4\xbc\xbc\xef\xbc\x9b\xe4\xbe\x8b\xe5\xa6\x82\xef\xbc\x9b\xe5\xa5\xbd\xe6\xaf\x94\xef\xbc\x9b\xe5\xa6\x82\xe5\x90\x8c\xef\xbc\x9b\xe7\xac\xa6\xe5\x90\x88\xe2\x80\xa6\xe7\x9a\x84\xe6\x96\xb9\xe5\xbc\x8f\xef\xbc\x9b\xe8\x83\xbd\xe6\x96\x99\xe6\x83\xb3\xe5\x88\xb0\xe7\x9a\x84\xef\xbc\x9b\xe6\x9c\x89\xe2\x80\xa6\xe7\x89\xb9\xe7\x82\xb9\xe7\x9a\x84\xef\xbc\x9b\xe5\x90\x8c\xe2\x80\xa6 \xe4\xb8\x80\xe6\xa0\xb7\xef\xbc\x9b\xe5\xa6\x82\xef\xbc\x9b\xe5\xa5\xbd\xe5\x83\x8f\xef\xbc\x9b\xe5\x90\x8c\xe6\xa0\xb7\xe7\x9a\x84\xe4\xba\xba\xef\xbc\x9b\xe5\x96\x9c\xe7\x88\xb1\xef\xbc\x9b\xe5\x90\x8c\xe6\xa0\xb7\xe7\x9a\x84\xe4\xba\x8b\xef\xbc\x9b\xe7\x9b\xb8\xe5\x90\x8c\xe7\x9a\x84\xef\xbc\x9b\xe5\x90\x8c\xe6\xa0\xb7\xe7\x9a\x84\xef\xbc\x9b\xe9\x80\xbc\xe7\x9c\x9f\xe7\x9a\x84\xef\xbc\x9b\xe5\x83\x8f\xe7\x9a\x84\xef\xbc\x9b\xe4\xbb\xa5\xe2\x80\xa6\xe7\x9a\x84\xe6\x96\xb9\xe5\xbc\x8f\xef\xbc\x9b\xe5\xa5\xbd\xe5\x83\x8f\xe5\x9c\xa8\xe8\xaf\xb4\xef\xbc\x9b\xe5\x96\x9c\xe6\xac\xa2\xef\xbc\x9b\xe5\xb8\x8c\xe6\x9c\x9b\xef\xbc\x9b\xe6\x83\xb3\xe8\xa6\x81\xef\xbc\x9b\xe8\xae\xa4\xe4\xb8\xba\xef\xbc\x9b\xe4\xb8\x8d\xe6\x83\x85\xe6\x84\xbf\xef\xbc\x9b\xe6\x83\x85\xe6\x84\xbf\xe8\xa6\x81\xef\xbc\x9b\xe5\xae\x81\xe8\xa6\x81\xef\xbc\x9b\xe6\x84\x9f\xe8\xa7\x89"}],"isAutoMatch":false,"kana":""}}'
b'{"status":0,"info":"success","zly":"zly","node":"1","data":{"translate":{"zly":"zly","errorCode":"0","qc_type":"2","index":"content0","from":"en","source":"sogou","text":"I like FishC.com","to":"zh-CHS","id":"a5ffe05c-34dd-4467-8308-9cb786a9a93d","dit":"\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2FishC.com","orig_text":"I like FishC.com","diff_text":"\xee\x90\x8aI like FishC.com\xee\x90\x8b\xee\x90\x8cilikefishc.com\xee\x90\x8d","qc_text":"ilikefishc.com","md5":""},"detect":{"zly":"zly","detect":"en","errorCode":"0","language":"\xe8\x8b\xb1\xe8\xaf\xad","id":"573c5190-fa84-11eb-913f-31d41187ce12","text":"I like FishC.com"},"sgtkn":"CB157737AF83C5C3B3B67D8E1C7E84C5B48B72CD611394A3","wordCard":{"title":false,"show":false,"usual_Dict":"","second_query":"","exchange":"","levelList":""},"detail":"","book":"","network":"","pic":"","voice":"","keywords":[{"key":"like","value":"\xe5\x83\x8f\xef\xbc\x9b\xe7\x9b\xb8\xe4\xbc\xbc\xef\xbc\x9b\xe7\xb1\xbb\xe4\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\xe6\x83\x85\xe6\x84\xbf\xef\xbc\x9b\xe6\x83\x85\xe6\x84\xbf\xe8\xa6\x81\xef\xbc\x9b\xe5\xae\x81\xe8\xa6\x81\xef\xbc\x9b\xe6\x84\x9f\xe8\xa7\x89"}],"isAutoMatch":false,"kana":""}}'
{'data': {'book': '',
'detail': '',
'detect': {'detect': 'en',
'errorCode': '0',
'id': '573c5190-fa84-11eb-913f-31d41187ce12',
'language': '英语',
'text': 'I like FishC.com',
'zly': 'zly'},
'isAutoMatch': False,
'kana': '',
'keywords': [{'key': 'like',
'value': '像;相似;类似;例如;好比;如同;符合…的方式;能料想到的;有…特点的;同… '
'一样;如;好像;同样的人;喜爱;同样的事;相同的;同样的;逼真的;像的;以…的方式;好像在说;喜欢;希望;想要;认为;不情愿;情愿要;宁要;感觉'}],
'network': '',
'pic': '',
'sgtkn': 'CB157737AF83C5C3B3B67D8E1C7E84C5B48B72CD611394A3',
'translate': {'diff_text': '\ue40aI like '
'FishC.com\ue40b\ue40cilikefishc.com\ue40d',
'dit': '我喜欢FishC.com',
'errorCode': '0',
'from': 'en',
'id': 'a5ffe05c-34dd-4467-8308-9cb786a9a93d',
'index': 'content0',
'md5': '',
'orig_text': 'I like FishC.com',
'qc_text': 'ilikefishc.com',
'qc_type': '2',
'source': 'sogou',
'text': 'I like FishC.com',
'to': 'zh-CHS',
'zly': 'zly'},
'voice': '',
'wordCard': {'exchange': '',
'levelList': '',
'second_query': '',
'show': False,
'title': False,
'usual_Dict': ''}},
'info': 'success',
'node': '1',
'status': 0,
'zly': 'zly'}
>>>