当前位置:   article > 正文

python出现errorcode50_使用scrapy爬取有道翻译时返回errorcode:50,用requests就是正常的?...

python出现errorcode50_使用scrapy爬取有道翻译时返回errorcode:50,用requests就是正常的?...

使用scrapy爬取有道翻译时返回errorcode:50,用requests就是正常的

def get_result(self):

'''headers里面有一些参数是必须的,注释掉的可以不用带上'''

headers = {

'referer': 'https://www.baidu.com/',

'cookie': 'OUTFOX_SEARCH_USER_ID=-2022895048@10.168.8.76;',

'user-agent': 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36',

}

data = {'i': 'hello',

'from': 'AUTO',

'to': 'AUTO',

'smartresult': 'dict',

'client': 'fanyideskweb',

'salt': '1523323753717',

'sign': 'e9243107c4256bef73cd95bef15006e0',

'doctype': 'json',

'version': '2.1',

'keyfrom': 'fanyi.web',

'action': 'FY_BY_CL1CKBUTTON',

'typoResult': 'true'}

html = requests.post(self.url, data=data, headers=headers).text

print(html)

infos = json.loads(html)

if 'translateResult' in infos:

try:

result = infos['translateResult'][0][0]['tgt']

print(result)

except:

pass

以上是使用requests方式,代码是从鱼C抄的,我把其中data部分写死用于分析,是可以获得翻译结果,结果图如下

class FanyiSpider(scrapy.Spider):

name = 'fanyi'

allowed_domains = ['fanyi.baidu.com','translate.google.cn','fanyi.youdao.com']

start_url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"

def start_requests(self):

data = {'i': 'hello',

'from': 'AUTO',

'to': 'AUTO',

'smartresult': 'dict',

'client': 'fanyideskweb',

'salt': '1523323753717',

'sign': 'e9243107c4256bef73cd95bef15006e0',

'doctype': 'json',

'version': '2.1',

'keyfrom': 'fanyi.web',

'action': 'FY_BY_CL1CKBUTTON',

'typoResult': 'true'}

headers = {

'referer': 'https://www.baidu.com/',

'cookie': 'OUTFOX_SEARCH_USER_ID=-2022895048@10.168.8.76;',

'user-agent': 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36',

}

yield scrapy.FormRequest(

url=self.start_url,

formdata=data,

headers=headers,

callback=self.parse

)

def parse(self, response):

print("--" * 40)

print(response.body)

print("--" * 40)

以上是使用scrapy的代码,header和data都是相同的,但是返回的结果是errorcode:50???

是在是搞不懂,还没入门的菜鸟新手求教

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/855712
推荐阅读
相关标签
  

闽ICP备14008679号