python实现 成语,词语爬虫

效果如下


import random
import re
import time
import urllib.request

import xlwt.Workbook


def chengyu(url):
    try:
        pcres=urllib.request.urlopen(url)
        pcres=pcres.read().decode('gbk')
        return pcres
    #异常处理
    except urllib.error.HTTPError as reason:
        print(reason)
    

if __name__ == '__main__':
    zimu=["W"]
    #遍历字母
    for zm in zimu:
        #遍历数字
        rundetail_excel=xlwt.Workbook() 
        for num in range(10,14):
            print(zm+str(num))
            url="http://xxx.com/list/"+zm+"_"+str(num)+".html"
            res=chengyu(url)
            cyall=re.findall(r'<li><a href="/cy(\d+)/(\d+).html">(.+?)</a></li>',str(res))
            sheetname=zm+str(num)
            worksheet=rundetail_excel.add_sheet(sheetname,cell_overwrite_ok=True)
            worksheet.write(0,0,"idiom_name")
            worksheet.write(0,1,"idiom_word1")
            worksheet.write(0,2,"idiom_word2")
            worksheet.write(0,3,"idiom_word3")
            worksheet.write(0,4,"idiom_word4")
            worksheet.write(0,5,"idiom_lack")
            worksheet.write(0,6,"idiom_explan")
            worksheet.write(0,7,"idiom_option")
            #遍历每个成语
            for cylen in range(len(cyall)):
                time.sleep(1)
                print("编号",cylen)
                if len(cyall[cylen][2])==4:
                    cyname=cyall[cylen][2]
                    cypage=cyall[cylen][0]
                    cynum=cyall[cylen][1]
                    print("成语:",cyname)
                    cystr="五羖大夫五湖四海"
                    if cyname not in cystr:
                        worksheet.write(cylen+1,0,cyname)
                        jieshiurl="http://xxx.com/cy"+str(cypage)+"/"+str(cynum)+".html"
                        jieres=chengyu(jieshiurl)
                        jieinfo=re.findall('释义</td>(\s|[\r\n])+<td>(.+)</td>',str(jieres))
                        if len(jieinfo)>=1:
                            jieinfo=str(jieinfo[0])
                            jieinfo=jieinfo[8:-2]
                            worksheet.write(cylen+1,6,jieinfo)
                            worksheet.write(cylen+1,1,cyname[0])
                            worksheet.write(cylen+1,2,cyname[1])
                            worksheet.write(cylen+1,3,cyname[2])
                            worksheet.write(cylen+1,4,cyname[3])
                            randlack=[cyname[0],cyname[1],cyname[2],cyname[3]]
                            randint=[1,2,3,4]
                            intnum=random.choice(randint)
                            time.sleep(1)
                            #答案第一个 组词第二个
                            if intnum==1:
                                worksheet.write(cylen+1,5,cyname[0]) 
                                uni = cyname[1].encode('unicode-escape').decode()
                                uni=uni[2:]
                                url="http://www.xxx.com/zuci-"+str(uni)+"/"
                                pcres=urllib.request.urlopen(url)
                                pcres=pcres.read()
                                res=pcres.decode('utf-8', 'ignore')
                                time.sleep(1)
                                serinfo=re.findall(r'target="_blank">([\u4e00-\u9fa5])'+str(cyname[1])+'</a>',str(res))
                                if len(serinfo)>=5:
                                    resfive=serinfo[0]+serinfo[1]+serinfo[2]+serinfo[3]+serinfo[4]
                                    worksheet.write(cylen+1,7,resfive)
                            #答案第二个 组词第一个
                            elif intnum==2:
                                worksheet.write(cylen+1,5,cyname[1]) 
                                uni = cyname[0].encode('unicode-escape').decode()
                                uni=uni[2:]
                                url="http://www.xxx.com/zuci-"+str(uni)+"/"
                                pcres=urllib.request.urlopen(url)
                                pcres=pcres.read()
                                res=pcres.decode('utf-8', 'ignore')
                                time.sleep(1)
                                serinfo=re.findall(r'target="_blank">'+str(cyname[0])+'([\u4e00-\u9fa5])</a>',str(res))
                                if len(serinfo)>=5:
                                    resfive=serinfo[0]+serinfo[1]+serinfo[2]+serinfo[3]+serinfo[4]
                                    worksheet.write(cylen+1,7,resfive)
                            #答案第三个 组词第四个
                            elif intnum==3:
                                worksheet.write(cylen+1,5,cyname[2]) 
                                uni = cyname[3].encode('unicode-escape').decode()
                                uni=uni[2:]
                                url="http://www.xxx.com/zuci-"+str(uni)+"/"
                                pcres=urllib.request.urlopen(url)
                                pcres=pcres.read()
                                res=pcres.decode('utf-8', 'ignore')
                                time.sleep(1)
                                serinfo=re.findall(r'target="_blank">([\u4e00-\u9fa5])'+str(cyname[3])+'</a>',str(res))
                                if len(serinfo)>=5:
                                    resfive=serinfo[0]+serinfo[1]+serinfo[2]+serinfo[3]+serinfo[4]
                                    worksheet.write(cylen+1,7,resfive)
                            #答案第四个 组词第三个
                            elif intnum==4:
                                worksheet.write(cylen+1,5,cyname[3])
                                uni = cyname[2].encode('unicode-escape').decode()
                                uni=uni[2:]
                                url="http://www.xxx.com/zuci-"+str(uni)+"/"
                                pcres=urllib.request.urlopen(url)
                                pcres=pcres.read()
                                res=pcres.decode('utf-8', 'ignore')
                                time.sleep(1)
                                serinfo=re.findall(r'target="_blank">'+str(cyname[2])+'([\u4e00-\u9fa5])</a>',str(res))
                                if len(serinfo)>=5:
                                    resfive=serinfo[0]+serinfo[1]+serinfo[2]+serinfo[3]+serinfo[4]
                                    worksheet.write(cylen+1,7,resfive)
        
                            else:
                                pass 
                       
            rundetail_excel.save(zm+".xlsx")   
                    
                        新手学py编程


                    
            
    
    
    
    
    
    
    
    
    
    
    
    
    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

py编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值