Python 自用代码(知网会议论文网页源代码清洗)
#coding=utf-8
from pymongo import MongoClient
from lxml import etree
import requests jigou = u"\r\n 【机构】\r\n "
zuozhe = u"\r\n 【作者】\r\n " # 获取数据库
def get_db():
client = MongoClient('localhost', 27017)
db = client.cnki
db.authenticate("用户名","密码")
return db # 获取第num条数据
def get_data(table, num):
i = 1
for item in table.find({}, {"html":1,"_id":0}):
if i==num:
if item.has_key('html') and item['html']:
return item['html']
else:
i+=1
continue # 列表首元素转字符串
def list_str(list):
if len(list)!=0:
return list[0]
else:
return "" # 作者英文名,机构英文名
def en_ls(list, length1, length2):
if len(list)!=0:
list = list[0].replace(u"【Author】","").replace("\r\n","").strip().split(";")
if len(list)==(length2+length1)+1:
return list2str(list[:length1]), list2str(list[length1:-1])
else:
return "", ""
else:
return "", "" def hyxx(list):
if len(list)!=0:
hylmc,hymc,hysj,hydd,flh,zbdw = "","","","",[],""
for item in list:
if u"【会议录名称】" in item:
hylmc = item.replace(u"【会议录名称】","").replace("\r\n","").strip()
continue
if u"【会议名称】" in item:
hymc = item.replace(u"【会议名称】","").replace("\r\n","").strip()
continue
if u"【会议时间】" in item:
hysj = item.replace(u"【会议时间】","").replace("\r\n","").strip()
continue
if u"【会议地点】" in item:
hydd = item.replace(u"【会议地点】","").replace("\r\n","").strip()
continue
if u"【分类号】" in item:
flh = item.replace(u"【分类号】","").replace("\r\n","").strip()
continue
if u"【主办单位】" in item:
zbdw = item.replace(u"【主办单位】","").replace(u"、",";").replace("\r\n","").strip()
continue
return hylmc,hymc,hysj,hydd,flh,zbdw
else:
return "","","","","","" # 列表转字符串
def list2str(list):
if len(list)!=0:
return ";".join(list)
else:
return "" # 构造论文入库字典
def standard_dict(html):
dc = {}
print 1
# print html
tree = etree.HTML(html)
# 论文名称
dc["title"] = list_str(tree.xpath("//span[@id='chTitle']/text()"))
# 外文名称
dc["title_eng"] = list_str(tree.xpath("//span[@id='enTitle']/text()"))
# 作者
dc["author"] = list2str(tree.xpath("//p[text()='%s']/a/text()"%zuozhe))
# 作者数量
length1 = len(tree.xpath("//p[text()='%s']/a/text()"%zuozhe))
# 机构名称
dc["organization"] = list2str(tree.xpath("//p[text()='%s']/a/text()"%jigou))
# 机构数量
length2 = len(tree.xpath("//p[text()='%s']/a/text()"%jigou))
# 作者英文名, 机构英文名
dc["author_eng"], dc["organization_eng"] = en_ls(tree.xpath("//p[@id='au_en']/text()"), length1, length2)
# 摘要
dc["summary"] = list_str(tree.xpath("//span[@id='ChDivSummary']/text()"))
# 英文摘要
dc["summary_eng"] = list_str(tree.xpath("//span[@id='EnChDivSummary']/text()"))
# 关键词
dc["keywords"] = list2str(tree.xpath("//div[@class='keywords']/span[1]/a/text()"))
# 英文关键词
dc["keywords_eng"] = list2str(tree.xpath("//div[@class='keywords']/span[2]/a/text()"))
# 会议信息
dc["proceeding_title"],dc["conference_title"],dc["conference_date"],dc["conference_place"],dc["huiyflh"],dc["conference_org"] = hyxx(tree.xpath("//div[@class='summary']/ul/li/text()"))
if dc["proceeding_title"]=="":
print 2
dc["proceeding_title"] = list_str(tree.xpath("//div[@class='summary']/ul[1]/li/a/text()")) return dc # 主函数
def main():
db = get_db()
collection=db.conference
collection2 = db.conference_cleaned
for item in collection.find({}, {"html":1,"_id":0}):
if item.has_key('html') and item['html']:
dc = standard_dict(item['html'])
collection2.insert(dc) if __name__ == '__main__':
main()
# 以下代码用于测试清洗特定一条数据
# db = get_db()
# collection=db.conference
# data = get_data(collection, 1)
# dc = standard_dict(data)
# for k,v in dc.items():
# print k,v
Python 自用代码(知网会议论文网页源代码清洗)的更多相关文章
- Python 自用代码(某方标准类网页源代码清洗)
用于mongodb中“标准”数据的清洗,数据为网页源代码,须从中提取: 标准名称,标准外文名称,标准编号,发布单位,发布日期,状态,实施日期,开本页数,采用关系,中图分类号,中国标准分类号,国际标准分 ...
- python爬取中国知网部分论文信息
爬取指定主题的论文,并以相关度排序. #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests import linecache impor ...
- Python 自用代码(递归清洗采标情况)
将‘ISO 3408-1-2006,MOD ISO 3408-2-1991,MOD ISO 3408-3-2006,MOD’类似格式字符串存为: [{'code': 'ISO 3408-1-200 ...
- Python 自用代码(调整日期格式)
2017年6月28日 to 2017-06-282017年10月27日 to 2017-10-272017年12月1日 to 2017-12-012017年7月1日 to 2017-07-01 #co ...
- Python 自用代码(拆分txt文件)
现有一个28G的txt文件,里面每一行是一个分词过的专利全文文档,一共370多万行.我需要把它按每五万行为单位做成一个json文件,格式大致如下: [{"id":"100 ...
- Python 自用代码(scrapy多级页面(三级页面)爬虫)
2017-03-28 入职接到的第一个小任务,scrapy多级页面爬虫,从来没写过爬虫,也没学过scrapy,甚至连xpath都没用过,最后用了将近一周才搞定.肯定有很多low爆的地方,希望大家可以给 ...
- 论文 查重 知网 万方 paperpass
相信各个即将毕业的学生或在岗需要评职称.发论文的职场人士,论文检测都是必不可少的一道程序.面对市场上五花八门的检测软件,到底该如何选择?选择查重后到底该如何修改?现在就做一个知识的普及.其中对于中国的 ...
- 如何将知网下载的caj文件转换为pdf文件
一.问题描述: 最近在知网搜索论文的时候,经常遇到有的论文没有pdf文件的情况,但不得不吐槽我觉得知网做的阅读器确实是有点烂.所以想将caj文件转化为pdf文件,找到了一个比较好的方法,所以希望记录一 ...
- Python开源爬虫项目代码:抓取淘宝、京东、QQ、知网数据--转
数据来源:数据挖掘入门与实战 公众号: datadw scrapy_jingdong[9]- 京东爬虫.基于scrapy的京东网站爬虫,保存格式为csv.[9]: https://github.co ...
随机推荐
- MatserDetail自动展开
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...
- sudo cd为什么不能够执行
问题描述 我想要cd到/etc/docker,但是它给我一个权限不够的错误,然后,我想到使用sudo cd /etc/docker时,它告诉我sudo: cd:找不到命令. 于是,郁闷的我就去上网找了 ...
- 阿里云OSS C#回调服务实例代码
先贴出客户端上传文件代码和毁掉函数的定义 需要的引用有:using Aliyun.OSS: 通过nuget包,获取aliyun.oss dll string url = "http:// ...
- 剑指offer 面试题5 : 从尾到头打印链表
题目: 输入一个链表的头结点,从尾到头反过来打印出每个节点的值.链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; }; 思路: 通常 ...
- Django-ContentType
背景:学位课.专题课.价格策略(每一种课程(学位课和专题课下可分为不同的种类的课程)在不同学习时间内的价格不同) 例如:如何将课程表与价格策略表关联起来: 用外键是可以将课程表和价格策略表关联起来的, ...
- 【hdoj_1049】Climbing Worm
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1049 以 上升-下降 一次为一个周期,一个周期时间为2分钟,每个周期上升距离为(u-d).先只考虑上升,再 ...
- AC日记——codeforces Ancient Berland Circus 1c
1C - Ancient Berland Circus 思路: 求出三角形外接圆: 然后找出三角形三条边在小数意义下的最大公约数; 然后n=pi*2/fgcd; 求出面积即可: 代码: #includ ...
- tomcat中reloadable作用
reloadable作用 reloadable:如果这个属性设为true,tomcat服务器在运行状态下会监视在WEB-INF/classes和WEB-INF/lib目录下class文件的改动,如果监 ...
- 山东省第七届省赛 D题:Swiss-system tournament(归并排序)
Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...
- ubuntu 启动 重启 停止 apache
一.Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 start or $ sudo /etc/init.d/apache2 start ...