zoukankan      html  css  js  c++  java
  • 爬虫1

    #练习入门
    import urllib.request
    response = urllib.request.urlopen("http://blog.fishc.com/")
    html = response.read()
    print(html)
    
    #爬虫图片
    import urllib.request
    response = urllib.request.urlopen("http://placekitten.com/g/500/600")
    cat_img = response.read()
    with open('cat_500_600','wb')as f:
        f.write(cat_img)'''
    
    #有道翻译
    import urllib.request
    import urllib.parse  #parse解析
    import json
    while True:
        content = input("请输入需要翻译的内容(按q!退出程序):")
        if content == 'q!':
            break
        url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
        data = {}
        data['i']= content
        data['from']='AUTO'
        data['to']='AUTO'
        data['smartresult']='dict'
        data['client']='fanyideskweb'
        data['salt']=1513569368760
        data['sign']='de9f67e5eb175603a1a18ab02d28da35'
        data['doctype']='json'
        data['version']='2.1'
        data['keyfrom']='fanyi.web'
        data['action']='FY_BY_REALTIME'
        data['typoResult']='false'
        #encode硬编码
        data = urllib.parse.urlencode(data).encode('utf-8')
        response = urllib.request.urlopen(url,data)
        #将接收来的utf-8页面解码为unicode
        html = response.read().decode('utf-8')
        #打印出来的是字典格式
        #print(html)
        target = json.loads(html)
        print("翻译结果是:%s" % (target['translateResult'][0][0]['tgt']))

    练习有道翻译时,遇见两个坑,一个是url里面_o不去掉的话报错为:50,第二是我把content变量加了单引号,变成了字符串。
  • 相关阅读:
    MySQL5.6 GTID、多线程复制
    WPS for Linux(ubuntu)字体配置(字体缺失解决办法)
    linux下用phpize给PHP动态添加扩展
    Zabbix汉化方法
    [FTP] Pure-FTPd SSL/TLS 配置方法
    PHP 缓存扩展opcache
    sftp搭建
    nginx https使用
    iptables基本规则
    kvm虚拟机安装
  • 原文地址:https://www.cnblogs.com/huaerye/p/8059005.html
Copyright © 2011-2022 走看看