import requests import re import time from SQLServers import MSSQL import pymssql from pyquery import PyQuery as pq #抓取页面Html def GetHtml(url,page): time.sleep(3) print("当前页%d"%(int(page)+1)) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36' } fromContent = { '__EVENTTARGET': 'ctl00$ContentPlaceHolder1$GridViewData$ctl13$ddlJumpToPageNum', '__VIEWSTATE': '/wEPDwULLTE1ODA5ODgxODgPZBYCZg9kFgICAw9kFgICAQ9kFgYCAQ8QDxYGHg1EYXRhVGV4dEZpZWxkBQROYW1lHg5EYXRhVmFsdWVGaWVsZAUFWUNvZGUeC18hRGF0YUJvdW5kZ2QQFQQIPeWFqOmDqD0M6YOo5bGe6auY5qChDOacrOenkemrmOagoQzpq5jogYzpq5jkuJMVBAAEMDEwMgQwMTAzBDAxMDQUKwMEZ2dnZ2RkAgUPEA8WBh8ABQRuYW1lHwEFBXhjb2RlHwJnZBAVBAg95YWo6YOoPQbmqKrlkJEG57q15ZCRBuagoeWGhRUEAAgwMjEzMDEwMQgwMjEzMDEwMggwMjEzMDEwMxQrAwRnZ2dnZGQCCw88KwANAQAPFgQfAmceC18hSXRlbUNvdW50AtYLZBYCZg9kFhgCAQ9kFhBmD2QWAmYPFQIDMTQyCDIxMTAxMDUwZAIBD2QWAmYPFQJa5rCu5p2C546v5Y2h5a6+6ZOx44CB6ZOR5aSn546v5YyW5ZCI54mp55qE5ZCI5oiQ44CB57uT5p6E5Y+K5YKs5YyW56GF5rCi5Yqg5oiQ5Y+N5bqU56CU56m2WuawruadgueOr+WNoeWuvumTseOAgemTkeWkp+eOr+WMluWQiOeJqeeahOWQiOaIkOOAgee7k+aehOWPiuWCrOWMluehheawouWKoOaIkOWPjeW6lOeglOeptmQCAg9kFgJmDxUCBue6teWQkQbnurXlkJFkAgMPZBYCZg8VAhvlm73lrrboh6rnhLbnp5Hlrabln7rph5Hlp5Qb5Zu95a626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5AzExMAMxMTEDMTEyAzExMwMxMTQDMTE1AzExNgMxMTcDMTE4AzExOQMxMjADMTIxAzEyMgMxMjMDMTI0AzEyNQMxMjYDMTI3AzEyOAMxMjkDMTMwAzEzMQMxMzIDMTMzAzEzNAMxMzUDMTM2AzEzNwMxMzgDMTM5AzE0MAMxNDEDMTQyAzE0MwMxNDQDMTQ1AzE0NgMxNDcDMTQ4AzE0ORQrA5YBZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnFgFmZBgCBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUjY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRidG5TZWFyY2gFJmN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcjEkR3JpZFZpZXdEYXRhDzwrAAoBCAKWAWThRBCCQv705jTgfanMwoOAORKRLg==', 'ctl00$ContentPlaceHolder1$GridViewData$ctl13$ddlJumpToPageNum': page } try: response = requests.post(url=url, data=fromContent,headers=headers, timeout=10) response.encoding=response.apparent_encoding if response.status_code==200: print("获取源码成功") except Exception as e: print('获取源码失败:%s'%e) return response.text #获取子页面HTML def Get_ZIHtml(url): try: time.sleep(2) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36' } response=requests.get(url=url,headers=headers) response.encoding = response.apparent_encoding print('获取子页面成功') except Exception as e: print('获取子页面失败%s'%e) return response.text #获取页面总页数 def Get_Pagenumber(url,page): try: html = GetHtml(url, page) doc = pq(html) pageItm = doc('#ctl00_ContentPlaceHolder1_GridViewData .td0 tr td') page = re.findall(r",共(.+?)页", pageItm.text())[0] print('获取页数成功') except Exception as e: print('获取页数失败%s'%e) return page #获取Html遍历所有数据 def Get_Data(page,url): i = 0 LXXXList = [] # 立项信息 FZRList=[] # 项目负责人及课题组成员 JFYSList=[] # 经费预算及执行 JFLJList=[] # 经费累计收支情况 JFZCList=[] # 经费支出 KYCGList=[] # 科研成果 DESPList=[] # 大额设备和材料名称和价格 XMYSList=[] # 结题项目验收 SYList = [] # 首页数据 while i < int(page): html = GetHtml(url, i) doc = pq(html) print(i) # 项目主表标题 bodyItem = doc('#ctl00_ContentPlaceHolder1_GridViewData .hashover') for x in bodyItem('tr'): #子页面数据 ZIurl = 'http://kyjf.zjedu.gov.cn/kyjfgk/%s' % pq(x).find('td a').attr('href') ZIhtml = Get_ZIHtml(ZIurl) ZIdoc = pq(ZIhtml,parser="html") #立项信息 LXXX=ZIdoc('.tablelist1').children().children().children() Tup_LXXX = ( pq(x).find('td').eq(0).text(),#所属项目的项目编号 LXXX.eq(1).text(), #项目名称 LXXX.eq(3).text(), #立项部门 LXXX.eq(5).text(), #项目承担单位 LXXX.eq(7).text(), #项目协作单位 LXXX.eq(9).text(), #项目负责人 LXXX.eq(11).text(),#项目领域 LXXX.eq(13).text(),#立项时间 LXXX.eq(15).text(),#项目开始时间 LXXX.eq(17).text(),#项目结束时间 LXXX.eq(19).text(),#项目总额 LXXX.eq(21).text())#备注 LXXXList.append(Tup_LXXX) #项目负责人以及课题组成员 FZRTr = ZIdoc('#ctl00_ContentPlaceHolder1_GridViewData1 .hashover') for Ftr in FZRTr: Tup_FZR=( pq(x).find('td').eq(0).text(),#所属项目的项目编号 pq(Ftr).children().eq(1).text(),#姓名 pq(Ftr).children().eq(2).text(),#职称 pq(Ftr).children().eq(3).text(),#工作单位 pq(Ftr).children().eq(4).text()#承担任务 ) FZRList.append(Tup_FZR) #经费预算及执行 JFYSTr = ZIdoc('#ctl00_ContentPlaceHolder1_GridViewData9 .hashover') for JFYSTh in JFYSTr: Tup_JFYS=( pq(x).find('td').eq(0).text(),#所属项目的项目编号 pq(JFYSTh).children().eq(1).text(),#科目编号 pq(JFYSTh).children().eq(2).text(),#科目名称 pq(JFYSTh).children().eq(3).text(),#支出用途 pq(JFYSTh).children().eq(4).text(),#预算经费(元) pq(JFYSTh).children().eq(5).text())#实际支出(元) JFYSList.append(Tup_JFYS) #经费累计收支情况 JFLJTr = ZIdoc('#ctl00_ContentPlaceHolder1_GridViewData2 .hashover') for JFLJTh in JFLJTr: Tup_JFLJ=( pq(x).find('td').eq(0).text(),#所属项目的项目编号 pq(JFLJTh).children().eq(1).text(),#项目经费名称 pq(JFLJTh).children().eq(2).text(),#经费类型 pq(JFLJTh).children().eq(3).text(),#经费来源 pq(JFLJTh).children().eq(4).text(),#已收入(元) pq(JFLJTh).children().eq(5).text())#实际支出(元) JFLJList.append(Tup_JFLJ) #经费支出 JFZCTitle = ZIdoc('ul[class="listtab"] li') y = 1 for JFZC in JFZCTitle.items(): JFZCTr = ZIdoc('#ctl00_ContentPlaceHolder1_UcJf%d_GridViewData .hashover' % y) y = y + 1 for JFZCTh in JFZCTr: Tup_JFZC=( pq(x).find('td').eq(0).text(),# 所属项目的项目编号 JFZC.text(),#Table Title pq(JFZCTh).children().eq(1).text(),#支出科目名称 pq(JFZCTh).children().eq(2).text()#实际支出(元) ) JFZCList.append(Tup_JFZC) #科研成果 KYCGTr = ZIdoc('#ctl00_ContentPlaceHolder1_GridViewData4 .hashover') for KYCGTh in KYCGTr: Tup_KYCG=( pq(x).find('td').eq(0).text(), # 所属项目的项目编号 pq(KYCGTh).children().eq(1).text(), # 类型 pq(KYCGTh).children().eq(2).text(), # 编号 pq(KYCGTh).children().eq(3).text(), # 名称 pq(KYCGTh).children().eq(4).text() # 日期 ) KYCGList.append(Tup_KYCG) #大额设备和材料名称和价格 DESPTr = ZIdoc('#ctl00_ContentPlaceHolder1_GridViewData7 .hashover') for DESPTh in DESPTr: Tup_DESP=( pq(x).find('td').eq(0).text(), pq(DESPTh).children().eq(1).text(), pq(DESPTh).children().eq(2).text(), pq(DESPTh).children().eq(3).text(), pq(DESPTh).children().eq(4).text(), pq(DESPTh).children().eq(5).text(), pq(DESPTh).children().eq(6).text(), pq(DESPTh).children().eq(7).text() ) DESPList.append(Tup_DESP) # 结题项目验收 XMYS = ZIdoc('#ctl00_ContentPlaceHolder1_div8 .tablelist1 td') if XMYS.html() is not None: Tup_XMYS = ( pq(x).find('td').eq(0).text(), # 所属项目的项目编号 XMYS.eq(1).text(), # 验收组织单位 XMYS.eq(3).text(), # 验收类型 XMYS.eq(5).text(), # 验收日期 XMYS.eq(7).text(), # 验收组成员 XMYS.eq(9).text(), # 验收意见 XMYS.eq(11).text() # 备注 ) XMYSList.append(Tup_XMYS) #首页数据 Tup_SY=( i + 1, "http://kyjf.zjedu.gov.cn/kyjfgk/" + pq(x).find('td a').attr('href'), pq(x).find('td').eq(0).text(), pq(x).find('td').eq(1).text(), pq(x).find('td').eq(2).text(), pq(x).find('td').eq(3).text(), pq(x).find('td').eq(4).text(), pq(x).find('td').eq(5).text(), pq(x).find('td').eq(6).text(), pq(x).find('td').eq(7).text() ) SYList.append(Tup_SY) print("---第%d页完成---" % (int(i)+1)) i += 1 with pymssql.connect(".", "sa", "sa", "test") as conn: with conn.cursor(as_dict=True) as cursor: if len(LXXXList): cursor.executemany("INSERT INTO Tb_Article_LXXX " " (FID,ProjectName,ProjectUnit,ProjectDepartment,CollaborationUnit,ProjectLeader,ProjectArea,ProjectData" ",ProjectStartData,ProjectEndData ,ProjectTotal,Remarks)" " VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", LXXXList) conn.commit() if len(DESPList): cursor.executemany("INSERT INTO Tb_Article_DESP" "(FID,SubjectName,Specifications,Unit,Number,Price,Total,Remarks)" " VALUES(%s,%s,%s,%s,%s,%s,%s,%s)",DESPList) conn.commit() if len(FZRList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_FZR]" " ([FID]" " ,[Name]" " ,[ZhiCheng]" " ,[Unit]" " ,[Task])" " VALUES(%s,%s,%s,%s,%s)", FZRList) conn.commit() if len(JFLJList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_JFLJ]" " ([FID]" " ,[Name]" " ,[Type]" " ,[Come]" " ,[Income]" " ,[Funds])" " VALUES(%s,%s,%s,%s,%s,%s)", JFLJList) conn.commit() if len(JFYSList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_JFYS]" " ([FID]" " ,[Code]" " ,[Name]" " ,[Purpose]" " ,[Funds]" " ,[ActualFunds])" " VALUES(%s,%s,%s,%s,%s,%s)", JFYSList) conn.commit() if len(JFZCList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_JFZC]" " ([FID]" " ,[Title]" " ,[SubjectName]" " ,[Actual])" " VALUES(%s,%s,%s,%s)", JFZCList) conn.commit() if len(KYCGList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_KYCG]" " ([FID]" " ,[Type]" " ,[Number]" " ,[Name]" " ,[Data])" " VALUES(%s,%s,%s,%s,%s)", KYCGList) conn.commit() if len(SYList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_SY]" " ([AP_Page]" " ,[AP_Url]" " ,[AP_projectCode]" " ,[AP_projectName]" " ,[AP_projectType]" " ,[AP_LXBM]" " ,[AP_projectNuit]" " ,[AP_Data]" " ,[AP_Amount]" " ,[AP_Uname])" " VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", SYList) conn.commit() if len(XMYSList): cursor.executemany("INSERT INTO [test].[dbo].[Tb_Article_XMYS] " " ([FID]" " ,[AcceptanceUnit]" " ,[AcceptanceType]" " ,[AcceptanceData]" " ,[AcceptanceMember]" " ,[AcceptanceOpinion]" " ,[Remarks])" " VALUES(%s,%s,%s,%s,%s,%s,%s)", XMYSList) conn.commit() print('----数据获取成功!----') if __name__ == '__main__': try: url = "http://kyjf.zjedu.gov.cn/kyjfgk/Item_Xm_query.aspx" page=Get_Pagenumber(url,1) print(page) Get_Data(page,url) print('获取完成') except Exception as e: print('数据获取失败%s'%e)