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

#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. Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”

    Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向” 原因还未知..

  2. Mac-安装itellij idea

    1.安装解压 Unzip and copy JetbrainsCrack.jar to /Applications/CLion.app/Contents/bin/JetbrainsCrack.jar ...

  3. MySQL 多列索引优化小记

    MySQL 5.6.30 问题背景 由于爬虫抓取的数据不断增多,这两天在不断对数据库以及查询语句进行优化,其中一个表结构如下: CREATE TABLE `newspaper_article` ( ` ...

  4. mysql 使用set names 解决乱码问题

    引子: 最近查询公司线上表数据,返现在Xshell控制台打印的数据都是乱码,记得之前瞄过同事都是执行set names UTF8 , 解决的,特记录如下.

  5. Sikuli 安装使用之初体验(为Sikuli X指定jre路径)

    Sikuli 是一种新颖的图形脚本语言,在实际的自动化测试中如果仅仅依靠selenium 还是远远不够的,selenium自动化本身是存在着诸多缺陷的,基于浏览器之外的控件 (windows 控件 等 ...

  6. redis使用管道pipeline提升批量操作性能(php演示)

    Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处理命令并将响应发送回客户 ...

  7. php7使用curl

    /** * @param string $url * @return mixed */ public function doGet($url) { //初始化 $ch = curl_init(); c ...

  8. 山东省第八届省赛 A:Return of the Nim(尼姆+威佐夫)

    Problem Description Sherlock and Watson are playing the following modified version of Nim game: Ther ...

  9. centos6.5 卸载adobeflash

    # rpm -e flash-plugin # rpm -qa | grep ^flash-plugin

  10. 前端学习blog

    有一些js写的小游戏,很有趣  http://oldj.net