日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬取中国知网(中国优秀硕士学位论文数据库)

發布時間:2024/3/26 python 72 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬取中国知网(中国优秀硕士学位论文数据库) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

筆者這幾天受團隊任務安排,需要寫一份兒關于知網(中國優秀碩士學位論文數據庫?? 入口:http://gb.oversea.cnki.net/kns55/brief/result.aspx?dbPrefix=CMFD

)的代碼,主要是爬取論文的作者信息、論文副標題、學位授予年度、學校、引用頻次等,例如在關鍵詞搜索一下醫療保險:

表格的相關信息都可以爬取,點進每一篇文章以后,對應的關鍵詞、副標題信息也可以爬取,只是筆者此次不需要設計下載原文,所以沒有寫相關代碼,但是整個分析格式是一樣的,可以根據關鍵詞的爬取方式去尋找下載的url。對于這個內容的爬取,似乎對于訪問的限制不太嚴格,筆者基本不考慮sleep時間,先貼出代碼:

# -*- coding:utf-8 -*- __author__ = 'TengYu' import requests import re from lxml import etree import json import time import xlwt import sys reload(sys) sys.setdefaultencoding('utf8')headers = { 'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0','Cookie':'Your-cookie' } class PageList(object):def __init__(self,maxpage):self.maxpage = maxpagedef getUrl(self):f1 = open("title_4_4.txt",'w')f2 = open("subtitle.txt",'w')f3 = open("keywords_4_4.txt",'w')excel = xlwt.Workbook(encoding='utf-8')sheet = excel.add_sheet('sheet1')sheet.write(0, 0, '論文中文題目')sheet.write(0, 1, '論文作者')sheet.write(0, 2, '作者學校')sheet.write(0, 3, '學位授予年度')sheet.write(0, 4, '下載頻次')sheet.write(0, 5, '引用頻次')begin = 6while begin < 18:sheet.write(0, begin, '關鍵詞'+str(begin - 6))begin += 1while begin < 25:sheet.write(0, begin, '副標題'+ str(begin - 18))begin += 1firstPage = 200try:tempurl = " "num = 0while firstPage < self.maxpage:tempurl = "http://gb.oversea.cnki.net/kns55/brief/brief.aspx?curpage="+str(firstPage)+"&RecordsPerPage=20&QueryID=2&ID=&turnpage=1&tpagemode=L&dbPrefix=CMFD&Fields=&DisplayMode=listmode&PageName=ASP.brief_result_aspx&sKuaKuID=2"response = requests.get(tempurl, headers=headers).contentselector = etree.HTML(response)trs = selector.xpath("//tr")firstTds = 11# print(len(trs))print("已經抓取"+str(num)+"條數據")while firstTds < 51:num = num + 1tr = trs[firstTds]td = tr.xpath(".//td")titletd = td[2]titlehref = titletd.xpath(".//a/@href")# print(titlehref)# print(titletd.xpath("string(.)"))href = str(titlehref[0])# print(href) #獲取論文詳細內容鏈接detailurl = "http://gb.oversea.cnki.net" + hrefprint(detailurl)authortd = td[3] # 獲取作者信息schooltd = td[4] # 獲取學校信息yeartd = td[5] # 獲取年份信息yinyongtd = td[6] # 獲取引用次數信息xiazaitd = td[7] # 獲取下載次數信息author = str(authortd.xpath("string(.)").encode('utf-8'))school = str(schooltd.xpath("string(.)").encode('utf-8'))year = str(yeartd.xpath("string(.)").encode('utf-8'))yinyong = "0"xiazai = "0"yinyongs = str(yinyongtd.xpath("string(.)").encode('utf-8'))xiazais = str(xiazaitd.xpath("string(.)").encode('utf-8'))if yinyongs.isspace():yinyong = "0"else:yinyong = yinyongs.replace(' ', '')if xiazais.isspace():xiazai = "0"else:xiazai = xiazais.replace(' ', '')firstTds += 2# 獲取具體數據信息,訪問構造的detailurldetail = requests.get(detailurl, headers=headers).contentsel = etree.HTML(detail)divs = sel.xpath("//div[@id='main']")title = divs[0].xpath("//div[@id='title']")[0]span = title.xpath("//h1//span")[0]st = str(span.xpath("string(.)").encode('utf-8'))print(st) # 論文題目divs = sel.xpath(".//div[@class='summary pad10']")[0]detailinfo = str(divs.xpath("string(.)").encode('utf-8'))ps = divs.xpath(".//p")f1.write(st+"\n")sheet.write(num, 0, st)sheet.write(num, 1, author)sheet.write(num, 2, school)sheet.write(num, 3, year)sheet.write(num, 4, xiazai)sheet.write(num, 5, yinyong)try:keywordsdiv = sel.xpath(".//div[@class='keywords']")[0]span = keywordsdiv.xpath(".//span[@id='ChDivKeyWord']")[0]hrefs = span.xpath(".//a")i = 0first = 6while i < len(hrefs):words = str(hrefs[i].xpath("string(.)").encode('utf-8'))print(words)f3.write(words+"\n")sheet.write(num, first, words)first += 1i += 1except Exception as es:print(es)try:if u'副題名' in detailinfo:index = detailinfo.find('【副題名】')loc = index + 15then = 18while detailinfo[loc] != '【':subtitile = ""while detailinfo[loc] != ' ' and detailinfo[loc] != ',':subtitile += detailinfo[loc]loc += 1sheet.write(num, then, subtitile)f2.write(subtitile + "\n")then += 1except Exception as es:print(es)time.sleep(1)firstPage += 1time.sleep(5)except Exception as es:print (es)excel.save("filename.xls")passpagelist = PageList(270) pagelist.getUrl()

筆者當初大概看了一下,好像搜索出來的那個表格的話,第一頁和后面的頁數的格式不太一樣,所以就從第二頁開始爬的,第一頁的代碼有興趣的讀者可以自行完成(當然也可能一樣,只是我沒仔細看,因為當時是上午布置任務,下午四點前提交數據,所以就省略過去了)在你完成相關搜索以后,代碼所分析的url(從第二頁開始)其實是“http://gb.oversea.cnki.net/kns55/brief/brief.aspx?curpage=3&RecordsPerPage=20&QueryID=3&ID=&turnpage=1&tpagemode=L&dbPrefix=CMFD&Fields=&DisplayMode=listmode&PageName=ASP.brief_result_aspx&sKuaKuID=3”,至于怎么分析這個數據是從哪個鏈接獲取的,比較簡單,就不在這里介紹了,但是提醒注意的是筆者中途因為有點事兒,斷了一下電腦,結果發現怎么都爬不下來了,一開始以為是代碼的問題,最后才發現是上面這個url中的"QueryID和sKuqKuID"發生了改變,從3變成了2,所以大家如果出現這種情況,一定記得再去數據獲取的url找一下這值,改成正確的一下。整個過程并不難理解,至于后面那個怎么查找具體的關鍵詞之類的,建議大家可以打開一個所有信息(包括關鍵詞,副標題)的論文的源代碼后,用個h5開發工具將代碼拷貝過去分析一下標簽就行了,筆者這里不再贅述。

尊重原作,轉載請注明,轉載自:https://blog.csdn.net/kr2563

總結

以上是生活随笔為你收集整理的python爬取中国知网(中国优秀硕士学位论文数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。