鱼C论坛

 找回密码
 立即注册
查看: 2508|回复: 2

零基础入门学习python,做有道词典的爬虫

[复制链接]
发表于 2017-9-27 15:52:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
win 10
python 3.6.2


代码如下:

import urllib.request
import urllib.parse
import json

while 1:
    content=str(input('请输入需要翻译的内容:'))

    url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
   
    head={}
    head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
        
    data={}


    data['i']=content
    data['from']='AUTO'
    data['to']='AUTO'
    data['smartresult']='dict'
    data['client']='fanyideskweb'
    data['salt']='1506486158432'
    data['sign']='7c1484518eee3f7628e3c7e6e09e8893'
    data['doctype']='json'
    data['version']='2.1'
    data['keyfrom']='fanyi.web'
    data['action']='FY_BY_REALTIME'
    data['typoResult']='true'

    data=urllib.parse.urlencode(data).encode('utf-8')

    response=urllib.request.urlopen(url,data,head)

    html=response.read().decode('utf-8')

    target=json.loads(html)
    trans=target['translateResult'][0][0]['tgt']
    print('翻译结果:%s'%(trans))


运行后错误提示如下:

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================== RESTART: E:\Python3.6.2\实验程序\50.2有道翻译.py ==================
请输入需要翻译的内容:haha
Traceback (most recent call last):
  File "E:\Python3.6.2\实验程序\50.2有道翻译.py", line 31, in <module>
    response=urllib.request.urlopen(url,data,head)
  File "E:\Python3.6.2\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "E:\Python3.6.2\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "E:\Python3.6.2\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "E:\Python3.6.2\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "E:\Python3.6.2\lib\urllib\request.py", line 1346, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "E:\Python3.6.2\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "E:\Python3.6.2\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "E:\Python3.6.2\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "E:\Python3.6.2\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "E:\Python3.6.2\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "E:\Python3.6.2\lib\http\client.py", line 964, in send
    self.connect()
  File "E:\Python3.6.2\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "E:\Python3.6.2\lib\socket.py", line 710, in create_connection
    sock.settimeout(timeout)
TypeError: an integer is required (got type dict)


请问问题出在哪里了?
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-27 18:03:50 | 显示全部楼层
import urllib.request
import urllib.parse
import json

while 1:
    content = str(input('请输入需要翻译的内容:'))

    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'

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

    data = {}

    data['i'] = content
    data['from'] = 'AUTO'
    data['to'] = 'AUTO'
    data['smartresult'] = 'dict'
    data['client'] = 'fanyideskweb'
    data['salt'] = '1506486158432'
    data['sign'] = '7c1484518eee3f7628e3c7e6e09e8893'
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_REALTIME'
    data['typoResult'] = 'true'

    data = urllib.parse.urlencode(data).encode('utf-8')

# 需要配置超时时间
    response = urllib.request.urlopen(url, timeout=60, data=data)

    html = response.read().decode('utf-8')

    target = json.loads(html)
    trans = target['translateResult'][0][0]['tgt']
    print('翻译结果:%s' % (trans))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 08:18:58 | 显示全部楼层
$DIM 发表于 2017-9-27 18:03
import urllib.request
import urllib.parse
import json

这个可以运行,我有一个问题想要请教,我的response中包含了url,data,head三项内容,结果不能运行,但是当我把程序中所有关于head的内容删掉或者变为注释时,程序就可以运行,不知道这是什么原因呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-24 03:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表