zoukankan      html  css  js  c++  java
  • python网页抓取之英汉字典

      linux的字典本人实在用起来不舒服(stardict挺不错的,但是界面好看些,功能简单易用就好了)

    ,在线翻译又得打开庞大的浏览器....就打算自己写个,但是时间有限,为了简单,

    还是用python抓取网页来的快些。本人用的必应字典。

    http://cn.bing.com/dict/?mkt=zh-CN&setlang=ZH

    获取某个单词解释url是:

    http://cn.bing.com/dict/search?q=word&go=&qs=bs&form=CM&mkt=zh-CN&setlang=ZH

    其中的红色word即是要翻译“word”单词。

    代码如下dic.py:python使用的是2.7.5版本

     1 #!/usr/bin/python                 
     2 import urllib2,sys
     3 from HTMLParser import HTMLParser
     4 from htmlentitydefs import name2codepoint
     5 
     6 class MyHTMLParser(HTMLParser): #解析html
     7     def  __init__(self):
     8         HTMLParser.__init__(self)
     9         self.t=False
    10         self.trans=[]
    11         self.pr=False
    12     def handle_starttag(self, tag, attrs):
    13         if tag=='div':
    14             for attr in attrs:
    15                 if attr==('class','hd_prUS') or 
    16                  attr==('class','hd_pr'):
    17                     self.pr=True
    18         if tag=='span':                       
    19             for attr in attrs:
    20                 if attr==('class','def'):
    21                     self.t=True
    22     def handle_data(self, data):
    23         if self.t:
    24             self.trans.append(data)
    25             self.t=False
    26         if self.pr:
    27             print data
    28             self.pr=False
    29     def getTrans(self):
    30         return self.trans
    31 class trans:
    32     _URL='http://cn.bing.com/dict/search'
    33     def __init__(self):
    34         self.url=trans._URL+"?q=%s&go=&qs=bs&form=CM&mkt=zh-CN&setlang=ZH"
    35         self.html=None
    36     def getHtml(self,word):  #获取单词翻译的网页
    37         self.url=self.url %word
    38         print word," :"
    39         req = urllib2.Request(self.url)
    40         fd=urllib2.urlopen(req)
    41         self.html=fd.read()
    42         self.html=unicode(self.html,'utf-8')
    43         fd.close()
    44     
    45     def parseHtml(self):  #解析调用
    46         parser = MyHTMLParser()
    47         self.html=parser.unescape(self.html)
    48         parser.feed(self.html)
    49         s=parser.getTrans()
    50         t=1
    51         for i in s:
    52             print t,'. ',i
    53             t+=1
    54 
    55 if __name__=='__main__':
    56         t=trans()
    57         t.getHtml(sys.argv[1])
    58         t.parseHtml()
    59         

    终端操作:

    05:07@:~/workspace$ ./dic.py action                                                                                                                                                                                                  
    action  :                                                                                                                                                                                                                            
    美 [ˈækʃ(ə)n]                                                                                                                                                                                                                         
    英 [ˈækʃ(ə)n]                                                                                                                                                                                                                         
    1 .  行动;行为;诉讼;战斗                                                                                                                                                                                                                     
    2 .  务必做                                                                                                                                                                                                                             
    3 .  动作;操作;作用

    终端用起来也不甚方便,有时间把代码搬进图形程序中.....pyQt还是kivy等图形库,

    用过pyQt,抽时间看看吧。

    本人的注释是写博客后来加上去的,可以直接删了,或者添加# -*- coding: utf-8 -*-

      对于html的解析另一个不错的模块库是BeautifulSoup,解析相当给力。但是需要自己安装

    这个库。和上面代码功能一样,但是使用BeautifulSoup解析的。代码如下:

     1 #!/usr/bin/python                 
     2 import urllib2,sys
     3 from BeautifulSoup import BeautifulSoup
     4 class MyHTMLParser:
     5     def __init__(self):
     6         self.trans=[]
     7         self.us=None
     8         self.uk=None
     9         self.define=True
    10     def feed(self,html):
    11         parsed_html=BeautifulSoup(html)
    12         self.us=parsed_html.find('div',{'class':'hd_prUS'}).text
    13         self.uk=parsed_html.find('div',{'class':'hd_pr'}).text
    14         self.us=self.us.replace(' ',' ')
    15         self.uk=self.uk.replace(' ',' ')
    16         print self.us,',',self.uk       
    17         self.define=parsed_html.findAll('span',{'class':'def'})
    18         for t in self.define:
    19             self.trans.append(t.text)
    20     def getTrans(self):
    21         return self.trans
    22 class trans:
    23     _URL='http://cn.bing.com/dict/search'
    24     def __init__(self):
    25         self.url=trans._URL+"?q=%s&go=&qs=bs&form=CM&mkt=zh-CN&setlang=ZH"
    26         self.html=None
    27     def getHtml(self,word):  
    28         self.url=self.url %word
    29         print word," :"
    30         req = urllib2.Request(self.url)
    31         fd=urllib2.urlopen(req)
    32         self.html=fd.read()
    33         fd.close()
    34     
    35     def parseHtml(self):  
    36         parser = MyHTMLParser()
    37         parser.feed(self.html)
    38         s=parser.getTrans()
    39         t=1
    40         for i in s:
    41             print t,'. ',i
    42             t+=1
    43 
    44 if __name__=='__main__':
    45         t=trans()
    46         t.getHtml(sys.argv[1])
    47         t.parseHtml()
    View Code
  • 相关阅读:
    DNNClassifier 深度神经网络 分类器
    浏览器对MP4视频 帧宽度 高度的兼容性
    UnicodeEncodeError:'latin-1' codec can't encode character
    文件夹下 文件计数
    the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
    the “identity” of an object
    广告特征 用户特征
    如果一个维度全覆盖,则有效维度应该对该维度全覆盖
    a high-level neural networks AP
    使用 LDA 挖掘的用户喜好主题
  • 原文地址:https://www.cnblogs.com/wuchaofan/p/3394124.html
Copyright © 2011-2022 走看看