爬科学基金共享服务网中基金数据

#coding=utf-8
import json
import requests
from lxml import etree
from HTMLParser import HTMLParser
from pymongo import MongoClient data = {'pageSize':10,'currentPage':1,'fundingProject.projectNo':'','fundingProject.name':'','fundingProject.person':'','fundingProject.org':'',
'fundingProject.applyCode':'','fundingProject.grantCode':'','fundingProject.subGrantCode':'','fundingProject.helpGrantCode':'','fundingProject.keyword':'',
'fundingProject.statYear':'','checkCode':'%E8%AF%B7%E8%BE%93%E5%85%A5%E9%AA%8C%E8%AF%81%E7%A0%81'}
url = 'http://npd.nsfc.gov.cn/fundingProjectSearchAction!search.action'
headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Content-Length':'',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'JSESSIONID=8BD27CE37366ED8022B42BFC68FF82D4',
'Host':'npd.nsfc.gov.cn',
'Origin':'http://npd.nsfc.gov.cn',
'Referer':'http://npd.nsfc.gov.cn/fundingProjectSearchAction!search.action',
'Upgrade-Insecure-Requests':'',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} def main():
client = MongoClient('localhost', 27017)
db = client.ScienceFund
db.authenticate("","")
collection=db.science_fund
for i in range(1, 43184):
print i
data['currentPage'] = i
result = requests.post(url, data = data, headers = headers)
html = result.text
tree = etree.HTML(html)
table = tree.xpath("//dl[@class='time_dl']")
for item in table:
content = etree.tostring(item, method='html')
content = HTMLParser().unescape(content)
# print content
bson = jiexi(content)
collection.insert(bson) def jiexi(content):
# 标题
title1 = content.find('">', 20)
title2 = content.find('</')
title = content[title1+2:title2]
# print title
# 批准号
standard_no1 = content.find(u'批准号', title2)
standard_no2 = content.find('</dd>', standard_no1)
standard_no = content[standard_no1+4:standard_no2].strip()
# print standard_no
# 项目类别
standard_type1 = content.find(u'项目类别', standard_no2)
standard_type2 = content.find('</dd>', standard_type1)
standard_type = content[standard_type1+5:standard_type2].strip()
# print standard_type
# 依托单位
supporting_institution1 = content.find(u'依托单位', standard_type2)
supporting_institution2= content.find('</dd>', supporting_institution1)
supporting_institution = content[supporting_institution1+5:supporting_institution2].strip()
# print supporting_institution
# 项目负责人
project_principal1 = content.find(u'项目负责人', supporting_institution2)
project_principal2 = content.find('</dd>', project_principal1)
project_principal = content[project_principal1+6:project_principal2].strip()
# print project_principal
# 资助经费
funds1 = content.find(u'资助经费', project_principal2)
funds2 = content.find('</dd>', funds1)
funds = content[funds1+5:funds2].strip()
# print funds
# 批准年度
year1 = content.find(u'批准年度', funds2)
year2 = content.find('</dd>', year1)
year = content[year1+5:year2].strip()
# print year
# 关键词
keywords1 = content.find(u'关键词', year2)
keywords2 = content.find('</dd>', keywords1)
keywords = content[keywords1+4:keywords2].strip()
# print keywords
dc = {}
dc['title'] = title
dc['standard_no'] = standard_no
dc['standard_type'] = standard_type
dc['supporting_institution'] = supporting_institution
dc['project_principal'] = project_principal
dc['funds'] = funds
dc['year'] = year
dc['keywords'] = keywords
return dc if __name__ == '__main__':
main()

Python 爬基金数据的更多相关文章

  1. python爬取数据需要注意的问题

    1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...

  2. python爬取数据保存到Excel中

    # -*- conding:utf-8 -*- # 1.两页的内容 # 2.抓取每页title和URL # 3.根据title创建文件,发送URL请求,提取数据 import requests fro ...

  3. python爬取数据保存入库

    import urllib2 import re import MySQLdb class LatestTest: #初始化 def __init__(self): self.url="ht ...

  4. Python 爬起数据时 'gbk' codec can't encode character '\xa0' 的问题

    1.被这个问题折腾了一上午终于解决了,再网上看到有用  string.replace(u'\xa0',u' ') 替换成空格的,方法试了没用. 后来发现 要在open的时候加utf-8才解决问题. 以 ...

  5. Python 爬取数据入库mysql

    # -*- enconding:etf-8 -*- import pymysql import os import time import re serveraddr="localhost& ...

  6. Python 爬取美团酒店信息

    事由:近期和朋友聊天,聊到黄山酒店事情,需要了解一下黄山的酒店情况,然后就想着用python 爬一些数据出来,做个参考 主要思路:通过查找,基本思路清晰,目标明确,仅仅爬取美团莫一地区的酒店信息,不过 ...

  7. 如何使用Python爬取基金数据,并可视化显示

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理 以下文章来源于Will的大食堂,作者打饭大叔 前言 美国疫情越来越严峻,大选也进入 ...

  8. python爬取股票最新数据并用excel绘制树状图

    大家好,最近大A的白马股们简直 跌妈不认,作为重仓了抱团白马股基金的养鸡少年,每日那是一个以泪洗面啊. 不过从金融界最近一个交易日的大盘云图来看,其实很多中小股还是红色滴,绿的都是白马股们. 以下截图 ...

  9. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

随机推荐

  1. 【bzoj1042】硬币购物

    容斥 #include<bits/stdc++.h> #define N 100005 typedef long long ll; using namespace std; ll ans, ...

  2. 第三方库SDWebImage的原理

    关于SDWebImage,其实是不用懂原理的,只是有一些面试官会问,分享给正在找工作的朋友们: 不多说直接上图: 另外..... 我的愿望是....... 世界和平.........

  3. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

  4. Selenium2+python自动化44-元素定位参数化(find_element)【转载】

    前言 元素定位有八种方法,这个能看到这一篇的小伙伴都知道了,那么有没有一种方法,可以把八种定位合为一种呢?也就是把定位的方式参数化,如id,name.css等设置为一个参数,这样只需维护定位方式的参数 ...

  5. iptables内网地外网之间访问

    环境:一台带外网和内网的机器,另一台只有内网,默认不能上网.两台机器都是centos系统带外网机器的外网ip为 123.221.20.11, 内网网关ip为 192.168.15.100内网机器的内网 ...

  6. java中Math.abs(-2147483648)的返回值应该是什么?

    我觉得这是一个非常有意思的问题,Math.abs(-2147483648)的返回值应该是什么? java计算结果 为什么没有得到正数结果呢? 首先我们先看下java区分整数正负的原理.在二进制的情况下 ...

  7. 【hdoj_1009】FatMouse's Trade

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1009< 本题用到贪心策略和结构体排序. 问题简化:现有资本M,N个房间,第i个房间对应着价格为F[i ...

  8. Python 进阶 之 lambda 匿名函数

    lambda 是个匿名函数,通常用于简单判断或者处理,例如判断一个数的奇偶性,过滤字符串,逻辑运算等等. lambda表达式: >>>lambda x:x*x >>> ...

  9. (4)三剑客之awk

    (1)awk工作原理#awk -F: '{print $1,$3}' /etc/passwd 1)awk使用一行作为输入,并将这一行赋给内部变量$0,每一行也可称为一个记录,已换行符结束 2)然后行被 ...

  10. 学习PHPCMS需要掌握的函数

    路径:phpcms\libs\classes\model.class.php /** * 执行sql查询 * @param $where 查询条件[例`name`='$name'] * @param ...