我无了(爬虫问题)
import urllib.request as rimport urllib.parse as p
import json as j
url = 'https://fanyi.youdao.com/translate_o?smartresult=dict,rule'
data = {}
#发出POST请求
data['i'] ="笑死"
data['from']="AUTO"
data['to']="AUTO"
data['smartresult']="dict"
data['client']="fanyideskweb"
data['salt']="16186631162494"
data['sign']="1316334dfcd2c44127e7d5894d55156e"
data['lts']="1618663116249"
data['bv']="1de9313c44872e4c200c577f99d4c09e"
data['doctype']="json"
data['version']="2.1"
data['keyfrom;']="fanyi.web"
data['action']="FY_BY_REALTlME"
data = p.urlencode(data).encode('utf-8')#把字典变成POST 注!!!:后面要用encode('utf-8')
resphonse = r.urlopen(url,data)#第一个是网页地址第二个是POST请求
html = resphonse.read().decode('utf-8')#linux一定要用decode('utf-8') decode是改变编码格式
print(html)
结果:
>>> %Run '有道翻译.py'
{"errorCode":50}
这里我的开发环境是linux 救救孩子吧 和小甲鱼演示的不一样 #coding:utf-8
from requests import post
import random,time
from hashlib import md5
#关键点:
#cookie 'OUTFOX_SEARCH_USER_ID=455539772@10.108.160.105'
#salt以及sign的算法
def cmd5(key):
m =md5()
m.update(key.encode('utf-8'))
return m.hexdigest()
def sign(key, salt):
_sign = 'fanyideskweb' + key + str(salt) + 'Tbh5E8=q6U3EXe+&L[4c@'
return cmd5(_sign)
def fanyi(key):
url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)',
'Cookie':'OUTFOX_SEARCH_USER_ID=455539772@10.108.160.105',
'Referer':'https://fanyi.youdao.com/'
}
salt = str(int(time.time()*1000) + random.randint(0, 10))
params = {
'i':key,
'from':'AUTO',
'to':'AUTO',
'client':'fanyideskweb',
'salt':salt,
'sign':sign(key, salt),
'doctype':'json',
'keyfrom':'fanyi.web'
}
res = post(url=url,headers=headers,data=params)
res = res.json()
translateResult = res.get('translateResult',['error'])
return translateResult
if __name__ == '__main__':
result = fanyi('笑死')
print(result)
我上有道看了下,人家的API明明是要求是get请求....
不好意思,刚刚找了个老的,新的API接口已经变了
网址: https://ai.youdao.com/DOCSIRMA/html/%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E7%BF%BB%E8%AF%91/API%E6%96%87%E6%A1%A3/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html
爬虫的声明周期很短,完全照着视频的来肯定是不行的 yuxijian2020 发表于 2021-4-18 10:02
爬虫的声明周期很短,完全照着视频的来肯定是不行的
谢谢
页:
[1]