基本信息
源码名称:python 调用词霸 翻译汉语为英文
源码大小:2.99KB
文件格式:.py
开发语言:Python
更新时间:2018-07-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍

import os
import requests as RS
from bs4 import BeautifulSoup as BS

def qqq(word):
# 金山词霸
    url = "http://www.iciba.com/"
    # word = input("输入你想要翻译的单词和词:")
    filename = open("lingyun.txt","a ")      

    if word == "":
            return "宝贝:不能为空哦!!!"
    

    try:
        page = RS.get(url word)
        con = page.content
        soup = BS(con,"lxml")
        key = soup.find("h1",class_="keyword")
        if key is None:
            key = soup.find("h1", style="width: 580px; margin-top: 15px; font-size: 15px; line-height: 24px; color: #897f6b;")
            if key is None:
                key = soup.find("h1", style="width: 580px; margin-top: 15px; font-size: 18px; line-height: 26px; color: #897f6b;")
                print(key.contents[0])
                filename.write(key.contents[0] "\n")
                lst = soup.find("div",style="width: 580px; margin-top: 15px; font-size: 15px; line-height: 24px; color: #333333;")
                print(lst.contents[0])
                filename.write(lst.contents[0] "\n")
                return '\n'.join(key.contents[0].split(','))  '\n'   '\n'.join(lst.contents[0].split(','))

            else:  
                print(key.contents[0])
                filename.write(key.contents[0] "\n") 
                lst = soup.find("div",style="width: 580px; margin-top: 15px; font-size: 18px; line-height: 24px; color: #333333;")
                print(lst.contents[0])
                filename.write(lst.contents[0] "\n")
                return '\n'.join(key.contents[0].split(','))  '\n'   '\n'.join(lst.contents[0].split(','))
        else:        
            print(key.contents[0])
            filename.write(key.contents[0] "\n")
            lst = soup.find_all("li",class_="clearfix")
            if len(lst) == 1:
                ans = lst
            else:
                ans = lst[:-1]
            for tag in ans:
                for item in tag.contents:
                    if item.name == "span":
                        print(item.contents[0])
                        filename.write(item.contents[0])
                        
                    elif item.name == "p":
                        s = ""
                        for i in item.contents:
                            if i != "\n":
                                print(i.contents[0])
                                s =i.contents[0] "\n"   

                                filename.writelines(i.contents[0])
                                filename.write("\n")
                                filename.flush()
                        return key.contents[0]   '\n'   item.contents[0]   ' \n'   s
    except AttributeError as e:
        print(e)
    except Exception:
        print("Something Wrong") 
    # word = input("输入你想要翻译的单词和词:") 
    filename.close();


 
# 定义函数
def printme( str ):
   "打印任何传入的字符串"
   print (str);
   return;
 
# 调用函数
printme("接下来我们开始翻译:");

qqq("例子");