多的不讲,标示很清楚,baidu搜索功能没开,匹配正则可以自己根据网页内容修改。xh-search1.0 for python

本文介绍了一个Python脚本,该脚本能够读取docx和txt格式的文档,并从百度搜索特定字符串的相关链接。通过解析和处理文档内容,脚本展示了如何进行文本预处理,包括编码转换、去除空白字符等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#!/usr/bin/env python
# coding=utf-8
import urllib2
import re
import string
import os
import time
import docx#引入docx包,需下载
#---读取docx文件------------------------------------
def readDocx(docName):
    fullText=[]
    doc=docx.Document(docName)
    paras=doc.paragraphs
    for p in paras:
        fullText.append(p.text)
    return '\n'.join(fullText)
#---读取docx文件-----------------------------------

#地址中指定文件名--------------------------------------------
def file_name(file_dir):   
        L=[]   
        for root, dirs, files in os.walk(file_dir):  
            for file in files:  
                if os.path.splitext(file)[1] == '.txt' or os.path.splitext(file)[1]=='.docx':  
                    L.append(os.path.join(root, file))  
        return L
#a=file_name(dir)
#for b in a:
#    print b
#地址中文件名指定------------------------------------



#搜索并匹配网页内容----------------------------------------------------------
def _search(word):
    urlfile=urllib2.urlopen('http://www.baidu.com/s?wd='+word,timeout=30)
    html=urlfile.read()

    html=html.replace(',',',')
    html=html.replace(':',':')
    word=word.replace(':',':')
    word=word.replace(',',',')
    #匹配相似文字和网址
    wordlist=re.findall(re.compile(""+word+"<\/em>.*href=\"http://www.*?\""),html)


    n=0
    #print wordlist
    #提取网址    
    w=""
    for w in wordlist:
        wnet=re.findall(re.compile("http.*"),w)
#        print word,wnet#输出名称和网址
        n +=1
    if n>0 and n<3:
        print '\033[1;31;40m'#颜色red
        print wnet
        print "搜索结果条目:",n
        print '\033[0m'#恢复
    return


#读取文件--------------------------------------------
#_load=raw_input("请输入文件地址:")
#_files=file_name(_load)
#for _file in _files:

#_site=open(_load,"r")
#i_input=_site.read()

#内容处理-----------------------------
def con_deal(i_input):
    #字码转换为utf8---------------------------------------------
    try:
        i_input=i_input.decode('gb2312').encode('utf-8')#gb2312-unicode-utf8    
    except Exception:
        i_input=i_input.decode('utf-8').encode('utf-8')#utf-8-unicode-utf8
    #字码转换为utf8-------------------------------------------

    i_input=i_input.replace("\r","")#除去回车
    i_input=i_input.replace("\n","")#除去换行
    i_input=i_input.replace(" ","")#除去空格
    i_input=i_input.replace("\t","")#除去tab
    return i_input
#    i_input=i_input.split("。")#以。分割文段
#内容处理---------------------------

dir=raw_input("输入文件夹目录:")
a=file_name(dir)#目录docx全部名单




for _single in a:
    print '\033[1;36;40m'#颜色blue 36蓝色33黄色,31红色35紫红
    print _single
    print '\033[0m'#恢复
    if os.path.splitext(_single)[-1] == '.txt':
        #print "txt"   
        _site=open(_single,"r")#读取单个文件内容
        _con=_site.read()
    elif os.path.splitext(_single)[-1]=='.docx':
        _single=readDocx(_single)
        _con=_single.encode('utf-8')#unicode-utf8
        #print "docx"
    
    _con=con_deal(_con)#处理单个内容

    _con=_con.split("。")#以。分割文段

    for x in _con:
        if len(x)>10:
    #一个中文字符三个长度,其他一个
            print "开始搜索句子:",x
#            _search(x)#此处开启百度查询
#        time.sleep(1)#延时一秒
#-----读取文件--------------------------------------- 

多的不讲,标示很清楚,baidu搜索功能没开,匹配正则可以自己根据网页内容修改。xh-search1.0 for python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑森林法则

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值