使用正则表达式从“中国古籍全录”网站自动提取文章,正则表达式古籍,我自己写的,第一次写这样
使用正则表达式从“中国古籍全录”网站自动提取文章,正则表达式古籍,我自己写的,第一次写这样
我自己写的,第一次写这样的东西。目前已经能用了,唯一的问题是古籍里面有很多不在unicode标准里的字符,遇到这些生僻的字会出错,貌似简繁转换厚会没有问题,但我不知道怎么做,还请各位指教。
#-*- coding:utf-8 -*-import osimport re #导入正则表达式模块import urllib.requesturl = input("请输入要抓取的文章主页面的地址:\\n")def dealurl(url): response = urllib.request.urlopen(url) html = response.read() uhtml = html.decode('utf-8') #将html解码出来 urls = re.findall('(?<=<li><a href=\\").*\\.html(?=\\">)',uhtml) #截取所有章节页面的url titles = re.findall('(?<=\\.html\\">).*(?=</a></li>)',uhtml) #截取所有章节标题 titleinlist = re.findall('(?<=title"><h3>).*(?=</h3></div>)',uhtml) #截取文章标题 kuinlist = re.findall('(?<=\\.html>).库(?=\\</a>)',uhtml) #截取文章所属的库 ku = kuinlist[0] title = titleinlist[0] print ("URL列表:\\n",urls) print ("章节列表:\\n",titles) if len(urls) == len(titles): num = len(urls) print ("解析出的链接数和章节数相等,匹配正确!\\n") print ("序章正文内容:\\n",text(uhtml)) dealurl2(url,'简介',title,ku) for i in range(num): url1 = urls[i] title1 = titles[i] dealurl2("http://guji.artx.cn" + url1,title1,title,ku) else: print ("解析出的链接数和章节数不相等,可能存在错误!\\n")def text(i): #处理文本 text1 = re.findall('(?<=font-size:14px;\\">).*(?=<br><font)',i,re.DOTALL) text1[0] = text1[0].replace("<br>","") if text1[0].count(" ") == 0: #判断有无html中的空格占位符 pass else: text1[0] = text1[0].replace(" ",' ') #若有,替换为空格 return text1[0]def dealurl2(i,n,m,k): #i是url,n是章节名,m是书的总标题,k是书籍所属库 response1 = urllib.request.urlopen(i) html1 = response1.read() uhtml1 = html1.decode('utf-8') #将html解码出来 if os.path.exists('E:/downloadedbooks/' + k + '/' + m) == False: os.makedirs('E:/downloadedbooks/' + k + '/' + m) else: pass article = text(uhtml1)#.encode('gbk') f = open('E:/downloadedbooks/' + k + '/' + m + '/' + n + '.txt','w') f.write(str(article)) f.close()dealurl(url)#该片段来自于http://byrx.net
相关内容
- 修改一个简单的爬虫,修改简单爬虫,import urlli
- Python读入文件并逐行处理,,import os ##
- Computes the integral with Gauss-Legendre quadrature using m nodes in
- python去除所有html标签,python去除标签,import rehtm
- python阳历转换成阴历的代码,python阳历,#! -*- encod
- python将unix时间戳转换成python的datetime格式,pythondateti
- python通过两张图片生成圆角图标效果,,flower = Ima
- Python生成随机数,python随机数,import rando
- Python调用cmd复制文件,python调用cmd,Python3 win7
- 模拟百度登录,模拟百度,最近公司产品和百度贴吧合
评论关闭