君子好逑 发表于 2020-8-9 23:13:03

爬虫

import urllib.request
import urllib.parse
import json
import time

while True:
    content = input('请输入待翻译的内容(输入"q!"退出程序):')
    if content == 'q!':
      break
   
    url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/"

    '''
    head = {}
    head['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
    '''

    data = {}
    data['type'] = 'AUTO'
    data['i'] = content
    data['doctype'] = 'json'
    data['xmlVersion'] = '1.6'
    data['keyfrom'] = 'fanyi.web'
    data['ue'] = 'UTF-8'
    data['typoResult'] = 'true'
    data = urllib.parse.urlencode(data).encode('utf-8')

    req = urllib.request.Request(url, data)
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36')

    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')

    target = json.loads(html)
    target = target['translateResult']['tgt']

    print(target)
    time.sleep(5)

有大佬有能爬有道的爬虫吗,小甲鱼这个不行了

Twilight6 发表于 2020-8-9 23:18:01



这样即可:

import urllib.request
import urllib.parse
import json
import time
while True:
    content=input('请输入待翻译的内容(输入"q!"退出程序):')
    if content=='q!':
      break
    head={}
    head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3706.400 SLBrowser/10.0.4040.400'
    url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    data={'i':content,
      'from':'AUTO',
      'to':'AUTO',
      'smartresult':'dict',
      'client':'fanyideskweb',
      'salt':'15966771704905',
      'sign': '8634c91db8413ef4cfbe684fd030d518',
      'ts': '1596677170490',
      'bv': 'd16528ec6ead722121051f646932f6ab',
      'doctype': 'json',
      'version': '2.1',
      'keyfrom': 'fanyi.web',
      'action':'FY_BY_REALTlME'}
    data=urllib.parse.urlencode(data).encode('utf-8')


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

    response=urllib.request.urlopen(req)
    html=response.read().decode('utf-8')

    target=json.loads(html)
    target=target['translateResult']['tgt']

    print(target)
    time.sleep(5)

君子好逑 发表于 2020-8-10 12:14:31

Twilight6 发表于 2020-8-9 23:18
这样即可:

谢谢大佬
页: [1]
查看完整版本: 爬虫