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 ...
随机推荐
- inno setup 5 添加快捷方式默认选中
转载:https://www.cnblogs.com/x_wukong/p/5012412.html https://zhidao.baidu.com/question/312006120.html ...
- isatty
isatty - test whether a file descriptor refers to a terminal #include <stdio.h> #include <u ...
- App云测试平台免费功能汇总
Wetest http://wetest.qq.com 阿里云测 https://mqc.aliyun.com/ Testbird https://www.testbird.com/ 百度 htt ...
- 用IJ和gradle启动elasticsearch5.4.3
环境准备 jdk gradle3.3+ idea git 从git clone源码 git checkout v5.4.3 打开项目 1. 在edit configurations添加new conf ...
- hdu 1054(最小点覆盖集)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- MySQL数据库中的Date,DateTime,int,TimeStamp和Time类型的对比
DATETIME 用在你需要同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD HH:MM:SS'格式显示DATETIME值,支持的范围是'1000-01-01 00:00:00 ...
- C++的Public.lib(Public.dll) : fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
今天开始编译网游服务器,找前辈借来批处理文件,版本控制上拿下代码,库等一系列资源,尼玛啊,编译出错: Public.lib(Public.dll) : fatal error LNK1112: mod ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- HDU 6216 A Cubic number and A Cubic Number【数学思维+枚举/二分】
Problem Description A cubic number is the result of using a whole number in a multiplication three t ...
- 线段树【p2706】贪婪大陆
Background 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败--人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前方是变异了的超 ...