#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 自用代码(知网会议论文网页源代码清洗)的更多相关文章

  1. Python 自用代码(某方标准类网页源代码清洗)

    用于mongodb中“标准”数据的清洗,数据为网页源代码,须从中提取: 标准名称,标准外文名称,标准编号,发布单位,发布日期,状态,实施日期,开本页数,采用关系,中图分类号,中国标准分类号,国际标准分 ...

  2. python爬取中国知网部分论文信息

    爬取指定主题的论文,并以相关度排序. #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests import linecache impor ...

  3. Python 自用代码(递归清洗采标情况)

    将‘ISO 3408-1-2006,MOD  ISO 3408-2-1991,MOD  ISO 3408-3-2006,MOD’类似格式字符串存为: [{'code': 'ISO 3408-1-200 ...

  4. 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 ...

  5. Python 自用代码(拆分txt文件)

    现有一个28G的txt文件,里面每一行是一个分词过的专利全文文档,一共370多万行.我需要把它按每五万行为单位做成一个json文件,格式大致如下: [{"id":"100 ...

  6. Python 自用代码(scrapy多级页面(三级页面)爬虫)

    2017-03-28 入职接到的第一个小任务,scrapy多级页面爬虫,从来没写过爬虫,也没学过scrapy,甚至连xpath都没用过,最后用了将近一周才搞定.肯定有很多low爆的地方,希望大家可以给 ...

  7. 论文 查重 知网 万方 paperpass

    相信各个即将毕业的学生或在岗需要评职称.发论文的职场人士,论文检测都是必不可少的一道程序.面对市场上五花八门的检测软件,到底该如何选择?选择查重后到底该如何修改?现在就做一个知识的普及.其中对于中国的 ...

  8. 如何将知网下载的caj文件转换为pdf文件

    一.问题描述: 最近在知网搜索论文的时候,经常遇到有的论文没有pdf文件的情况,但不得不吐槽我觉得知网做的阅读器确实是有点烂.所以想将caj文件转化为pdf文件,找到了一个比较好的方法,所以希望记录一 ...

  9. Python开源爬虫项目代码:抓取淘宝、京东、QQ、知网数据--转

    数据来源:数据挖掘入门与实战  公众号: datadw scrapy_jingdong[9]- 京东爬虫.基于scrapy的京东网站爬虫,保存格式为csv.[9]: https://github.co ...

随机推荐

  1. JavaSript中数组方法是否对原数组产生影响

    JavaScript中数组方法有很多.某次面试被问到,concat()方法会对影响到原数组吗.当时记得不牢,犹豫地说"会吧...".于是决定总结一下哪些数组方法会对原数组产生影响. ...

  2. P2654 原核生物培养

    P2654 原核生物培养 题目描述 W教授最近正在研究一种原核生物,这种生物的生长方式很奇特,只能通过吃掉同类而生长.两个该种生物相遇,较大质量的会把较小的吃掉(相同的话就看RP了),吃掉后较大的生物 ...

  3. net core服务器缺包,如何在线安装?

    Install -package命令不行. 下面命令也不行.求助大家,怎么安装?

  4. ANSI、ASCII、Unicode和UTF-8编码

    来自:http://blog.163.com/yang_jianli/blog/static/161990006201371451851274/ --------------------------- ...

  5. Selenium2+python自动化37-爬页面源码(page_source)【转载】

    前言 有时候通过元素的属性的查找页面上的某个元素,可能不太好找,这时候可以从源码中爬出想要的信息.selenium的page_source方法可以获取到页面源码. selenium的page_sour ...

  6. centos内核参数调优

    [net] ######################## cat /proc/sys/net/ipv4/tcp_syncookies # 默认值: # 作用:是否打开SYN Cookie功能,该功 ...

  7. hdu 1115(多边形重心问题)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 初探插头dp

    开学那个月学了点新东西,不知道还记不记得了,mark一下 感觉cdq的论文讲的很详细 题主要跟着kuangbin巨做了几道基础的 http://www.cnblogs.com/kuangbin/arc ...

  9. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  10. 【转载】bash: ifconfig: command not found 解决办法

    原本使用ifconfig 可以使用,今天是怎么了,可能安装软件修改了,百度~~ [oracle@localhost /]$ ifconfig 提示:“bash: ifconfig: command n ...