码迷,mamicode.com
首页 > 其他好文 > 详细

5-有道爬虫demo(post)

时间:2018-07-13 01:16:45      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:parse   python   script   raw   加密   英文   .com   lis   inpu   

爬取有道页面,实现中文翻译成英文:

#_*_ coding: utf-8 _*_

‘‘‘
Created on 2018-7-12 
@author: sss
功能:爬取有道翻译

‘‘‘
import urllib
import urllib.request
import urllib.parse
import urllib.response
from pip._vendor.urllib3.filepost import encode_multipart_formdata
from pip._vendor.distlib.compat import raw_input
import json
import random

# url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"  #http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule要把_o去掉

#user-agent列表,每次请求随机选一个:
ua_list = [
    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
    "Mozilla/5.0 (X11; CrOS i686 2268.111.0)like Gecko",
    "Mozilla/5.0 (Macintosh; U; PPC Mac OS X ",
    "Mozilla/5.0 (Macintosh; Intel Mac OS "
]

user_agnet = random.choice(ua_list)

headers = {
        "Connection" : "keep-alive",
        "Accept" : "application/json, text/javascript, */*; q=0.01",
        "X-Requested-With" : "XMLHttpRequest",
        "User-Agent" : user_agnet,
        "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
        "Referer" : "http://fanyi.youdao.com/"
    }

key = raw_input("请输入需要翻译的文字:\n")

formdata = {
        "i" :key,
        "from " :"AUTO",
        "to" :"AUTO",
        "smartresult" :"dict",
        "client" :"fanyideskweb",
        "salt" :"1531403738742",  #这个应该是个时间戳
        "sign" :"ffa2b29fe52953208226d97a174bcea7", #应该是根据时间戳+你要翻译的内容加密后生成的验证字段
        "doctype" :"json",
        "version" :"2.1",
        "keyfrom" :"fanyi.web",
        "action" :"FY_BY_REALTIME",
        "typoResult" :"false"
    }

data = urllib.parse.urlencode(formdata ).encode(encoding=‘UTF8‘)  #这里后面要加encoding=‘utf-8‘

request = urllib.request.Request(url, data = data, headers = headers)

html = urllib.request.urlopen(request).read()
print(html)

print(‘完成!‘)

target = json.loads(html)
print("翻译结果:%s"%(target[‘translateResult‘][0][0][‘tgt‘])) #读出结果

print(headers)

  

5-有道爬虫demo(post)

标签:parse   python   script   raw   加密   英文   .com   lis   inpu   

原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/9302596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!