美文网首页
简单爬虫进行拼音转汉字

简单爬虫进行拼音转汉字

作者: 小木胆 | 来源:发表于2017-04-26 10:04 被阅读0次

    论坛里面有人用想用搜索引擎进行拼音转汉字。

    目前已经有了百度的代码,就是要修改为google。

    很简单的东西,用urllib2+beautifulsoup就是分分钟的事情,但是考虑到这个哥们估计不会安装beautifulsoup,所以还是用re来解决:

    #!/usr/bin/env python
    # -*- coding: utf_8 -*-
    
    import re
    from urllib2 import Request, urlopen
    
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language':'en-US,en;q=0.8,zh-TW;q=0.6,zh;q=0.4,ja;q=0.2',
        'Cache-Control':'max-age=0',
        'Connection':'keep-alive',
        'DNT':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
    }  
    url_base = "https://www.google.co.jp/search?site=&source=hp&q=%s&oq=%s"
    
    def getpage(url):
        response = urlopen(Request(url = url,headers = headers), timeout = 60) 
        return response.read()
    
    def han(keyword):
        page = getpage(url_base % (keyword, keyword))
        result = re.findall('<a class="spell" href=.*?><em>(.*?)</em></a>', page) 
        ret = result[0] if len(result)>0 else ""
        return ret
    
    def getwords(input_file):
        with open(input_file) as f:
            raw_text = f.readlines()
        return [i.strip() for i in raw_text]
    
    
    if __name__ == "__main__":
        input_file  = '三拼四拼.txt'
        output_file = input_file.replace('.txt','_cn.txt')
        words = getwords(input_file)
    
        with open(output_file, 'w') as f:
            for word in words:
                cn = han(word)
                print '%s:%s\n'% (word, cn)
                f.write('%s:%s\n'% (word, cn))
        print '\n Done.'

    相关文章

      网友评论

          本文标题:简单爬虫进行拼音转汉字

          本文链接:https://www.haomeiwen.com/subject/yzqizttx.html