Python 爬基金数据
爬科学基金共享服务网中基金数据
#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 爬基金数据的更多相关文章
- python爬取数据需要注意的问题
1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...
- python爬取数据保存到Excel中
# -*- conding:utf-8 -*- # 1.两页的内容 # 2.抓取每页title和URL # 3.根据title创建文件,发送URL请求,提取数据 import requests fro ...
- python爬取数据保存入库
import urllib2 import re import MySQLdb class LatestTest: #初始化 def __init__(self): self.url="ht ...
- Python 爬起数据时 'gbk' codec can't encode character '\xa0' 的问题
1.被这个问题折腾了一上午终于解决了,再网上看到有用 string.replace(u'\xa0',u' ') 替换成空格的,方法试了没用. 后来发现 要在open的时候加utf-8才解决问题. 以 ...
- Python 爬取数据入库mysql
# -*- enconding:etf-8 -*- import pymysql import os import time import re serveraddr="localhost& ...
- Python 爬取美团酒店信息
事由:近期和朋友聊天,聊到黄山酒店事情,需要了解一下黄山的酒店情况,然后就想着用python 爬一些数据出来,做个参考 主要思路:通过查找,基本思路清晰,目标明确,仅仅爬取美团莫一地区的酒店信息,不过 ...
- 如何使用Python爬取基金数据,并可视化显示
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理 以下文章来源于Will的大食堂,作者打饭大叔 前言 美国疫情越来越严峻,大选也进入 ...
- python爬取股票最新数据并用excel绘制树状图
大家好,最近大A的白马股们简直 跌妈不认,作为重仓了抱团白马股基金的养鸡少年,每日那是一个以泪洗面啊. 不过从金融界最近一个交易日的大盘云图来看,其实很多中小股还是红色滴,绿的都是白马股们. 以下截图 ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
随机推荐
- 使用div实现progress进度条
在百度上搜了很多方法去修改HTML5 progress的样式,然而并没有实现. 所以自己用div实现了一个. 简单粗暴(*^-^*) 可以在CSS里改样式,可以JS里改进度. <div cla ...
- HashMap/Hashtable/ConcurrentHashMap区别
HashMap:每个隔间都没锁门,有人想上厕所,管理员指给他一个隔间,里面没人的话正常用,里面有人的话把这个人赶出来然后用. 优点,每个人进来不耽误都能用:缺点,每一个上厕所的人都有被中途赶出来的危险 ...
- python面向对象之__new__方法
众所周知,python中定义的类在创建实例对象的时候,会自动执行__init__()方法,但是在执行__init__()方法之前,会执行__new__()方法. __new__()的作用主要有两个. ...
- DRF视图集的路由设置
在使用DRF视图集时,往往需要配一大堆路由,例如: # views.py class DepartmentViewSet(ListModelMixin,CreateModelMixin,Retriev ...
- Redis Hlen 命令用于获取哈希表中字段的数量
http://www.runoob.com/redis/hashes-hlen.html
- MATLAB求函数零点与极值
1. roots函数 针对多项式求零点(详见MATLAB多项式及多项式拟合) 2. fzero函数 返回一元函数在某个区间内的的零点. x0 = fzero(@(x)x.^2-3* ...
- UTC时间
世界的每个地区都有自己的本地时间,在Internet及无线电通信时,时间的统一非常重要! 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用 ...
- office中密码的移除方法
密码有两种,一种是修改密码,一种是访问密码,通常两种密码的创建位置不同.对于访问密码,只需要在文件→信息 中将密码删除 对于修改密码,则要输入密码以后另存,然后在保存窗口里边点工具,下拉菜单里选常规选 ...
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- IDL软件初步了解
软件名称:IDL(交互式数据语言Interactive Data Language) 软件类别:是国产软件,语言类,数据分析类,数据可视化类 语言类别:解释型 作用:可以进行数据处理的可视化等,在大数 ...