使用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???
图片描述

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

阅读 3.8k
1 个回答
新手上路,请多包涵

没人回答。。。

推荐问题