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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

[python爬虫] BeautifulSoup和Selenium简单爬取知网信息测试

發(fā)布時間:2024/5/28 python 115 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [python爬虫] BeautifulSoup和Selenium简单爬取知网信息测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

作者最近在研究復(fù)雜網(wǎng)絡(luò)和知識圖譜內(nèi)容,準(zhǔn)備爬取知網(wǎng)論文相關(guān)信息進(jìn)行分析,包括標(biāo)題、摘要、出版社、年份、下載數(shù)和被引用數(shù)、作者信息等。但是在爬取知網(wǎng)論文時,遇到問題如下:
? 1.爬取內(nèi)容總為空,其原因是采用動態(tài)加載的數(shù)據(jù),無法定位,然后作者重新選取了CNKI3.0知網(wǎng)進(jìn)行了爬取;
? 2.但卻不含作者信息,需要定位到詳情頁面,再依次獲取作者信息,但是又遇到了新的問題。


一. 網(wǎng)站定位分析

知網(wǎng)網(wǎng)站如下:http://nvsm.cnki.net/kns/brief/default_result.aspx
比如搜索Python關(guān)鍵字,網(wǎng)頁反饋內(nèi)容如下所示,2681篇文章。


但是使用Selenium定位爬取的論文內(nèi)容總為空,后來網(wǎng)上看到qiuqingyun大神的博客,發(fā)現(xiàn)另一個知網(wǎng)接口(CNKI3.0 知識搜索:http://search.cnki.net/)。
強(qiáng)烈推薦大家閱讀他的原文:http://qiuqingyu.cn/2017/04/27/python實(shí)現(xiàn)CNKI知網(wǎng)爬蟲/
搜索python的的圖片如下,共3428篇論文。


接下來簡單講述分析的過程,方法都類似,通過DOM樹節(jié)點(diǎn)分析定位元素。右鍵瀏覽器審查元素如下所示,每頁包括15篇論文,標(biāo)簽位于<div class="wz_tab">下。


點(diǎn)擊具體一條內(nèi)容,如下所示,定位方法如下:
? 1.標(biāo)題定位<div class="wz_content">下的<h3>標(biāo)簽,并且可以獲取URL;
? 2.摘要定位<div class="width715">內(nèi)容;
? 3.出處定位<span class="year-count">節(jié)點(diǎn)下的title,年份通過正則表達(dá)式提取數(shù)據(jù);
? 4.下載次數(shù)和被引用數(shù)定位<span class="count">,提取數(shù)字第一個和第二個。



接下來直接講述BeautifulSoup和Selenium兩種方式的爬蟲。



二. BeautifulSoup爬蟲

BeautifulSoup完整代碼如下:

# -*- coding: utf-8 -*- import time import re import urllib from bs4 import BeautifulSoup#主函數(shù) if __name__ == '__main__':url = "http://search.cnki.net/Search.aspx?q=python&rank=relevant&cluster=all&val=&p=0"content = urllib.urlopen(url).read()soup = BeautifulSoup(content,"html.parser")#定位論文摘要wz_tab = soup.find_all("div",class_="wz_tab")num = 0for tab in wz_tab:#標(biāo)題title = tab.find("h3")print title.get_text()urls = tab.find("h3").find_all("a")#詳情超鏈接flag = 0for u in urls:if flag==0: #只獲取第一個URLprint u.get('href')flag += 1#摘要abstract = tab.find(attrs={"class":"width715"}).get_text()print abstract#獲取其他信息other = tab.find(attrs={"class":"year-count"})content = other.get_text().split("\n")"""由于無法分割兩個空格,如:《懷化學(xué)院學(xué)報》??2017年 第09期故采用獲取標(biāo)題titile內(nèi)容為出版雜志<span title="北方文學(xué)(下旬)">《北方文學(xué)(下旬)》??2017年 第06期</span>"""#出版雜志+年份cb_from = other.find_all("span")flag = 0 for u in cb_from:if flag==0: #獲取標(biāo)題print u.get("title")flag += 1mode = re.compile(r'\d+\.?\d*')number = mode.findall(content[0])print number[0] #年份#下載次數(shù) 被引次數(shù)mode = re.compile(r'\d+\.?\d*')number = mode.findall(content[1])if len(number)==1:print number[0]elif len(number)==2:print number[0], number[1]num = num + 1 輸出如下圖所示:


但是爬取的URL無法跳轉(zhuǎn),總是顯示登錄頁面,比如“http://epub.cnki.net/kns/detail/detail.aspx?filename=DZRU2017110705G&dbname=CAPJLAST&dbcode=cjfq”,而能正確顯示的是的“http://www.cnki.net/KCMS/detail/detail.aspx?filename=DZRU2017110705G&
dbname=CAPJLAST&dbcode=CJFQ&urlid=&yx=&v=MTc2ODltUm42ajU3VDN
mbHFXTTBDTEw3UjdxZVlPZHVGeTdsVXJ6QUpWZz1JVGZaZbzlDWk81NFl3OU16”。
顯示如下圖所示:


解決方法:這里我準(zhǔn)備采用Selenium技術(shù)定位超鏈接,再通過鼠標(biāo)點(diǎn)擊進(jìn)行跳轉(zhuǎn),從而去到詳情頁面獲取作者或關(guān)鍵詞信息。


三. Selenium爬蟲

爬取代碼如下:

# -*- coding: utf-8 -*- import time import re import sys import codecs import urllib from selenium import webdriver from selenium.webdriver.common.keys import Keys #主函數(shù) if __name__ == '__main__':url = "http://search.cnki.net/Search.aspx?q=python&rank=relevant&cluster=all&val=&p=0"driver = webdriver.Firefox()driver.get(url)#標(biāo)題content = driver.find_elements_by_xpath("//div[@class='wz_content']/h3")#摘要abstracts = driver.find_elements_by_xpath("//div[@class='width715']")#出版雜志+年份other = driver.find_elements_by_xpath("//span[@class='year-count']/span[1]")mode = re.compile(r'\d+\.?\d*')#下載次數(shù) 被引次數(shù)num = driver.find_elements_by_xpath("//span[@class='count']")#獲取內(nèi)容i = 0for tag in content:print tag.textprint abstracts[i].textprint other[i].get_attribute("title")number = mode.findall(other[i].text)print number[0] #年份number = mode.findall(num[i].text)if len(number)==1: #由于存在數(shù)字確實(shí) 如(100) ()print number[0]elif len(number)==2:print number[0],number[1]print ''i = i + 1tag.click()time.sleep(1) 輸出如下所示:
>>> 網(wǎng)絡(luò)資源輔助下的Python程序設(shè)計教學(xué) 本文對于Python學(xué)習(xí)網(wǎng)絡(luò)資源做了歸納分類,說明了每類資源的特點(diǎn),具體介紹了幾個有特色的學(xué)習(xí)網(wǎng)站,就網(wǎng)絡(luò)資源輔助下的Python學(xué)習(xí)進(jìn)行了討論,闡釋了利用優(yōu)質(zhì)網(wǎng)絡(luò)資源可以提高課堂教學(xué)效果,增加教學(xué)的生動性、直觀性和交互性。同時說明了這些資源的利用能夠方便學(xué)生的編程訓(xùn)練,使學(xué)生有更多的時間和機(jī)會動手編程,實(shí)現(xiàn)編程教學(xué)中... 電子技術(shù)與軟件工程 2017 11 0Python虛擬機(jī)內(nèi)存管理的研究 動態(tài)語言的簡潔性,易學(xué)性縮短了軟件開發(fā)人員的開發(fā)周期,所以深受研發(fā)人員的喜愛。其在機(jī)器學(xué)習(xí)、科學(xué)計算、Web開發(fā)等領(lǐng)域都有廣泛的應(yīng)用。在眾多的動態(tài)語言中,Python是用戶數(shù)量較大的動態(tài)語言之一。本文主要研究Python對內(nèi)存資源的管理。Python開發(fā)效率高,但是運(yùn)行效率常為人詬病,主要原因在于一切皆是對象的語言實(shí)現(xiàn)... 南京大學(xué) 2014 156 0 接下來是點(diǎn)擊詳情頁面,窗口轉(zhuǎn)化捕獲信息,代碼如下:
# -*- coding: utf-8 -*- import time import re import sys import codecs import urllib from selenium import webdriver from selenium.webdriver.common.keys import Keys #主函數(shù) if __name__ == '__main__':url = "http://search.cnki.net/Search.aspx?q=python&rank=relevant&cluster=all&val=&p=0"driver = webdriver.Firefox()driver.get(url)#標(biāo)題content = driver.find_elements_by_xpath("//div[@class='wz_content']/h3")#摘要abstracts = driver.find_elements_by_xpath("//div[@class='width715']")#出版雜志+年份other = driver.find_elements_by_xpath("//span[@class='year-count']/span[1]")mode = re.compile(r'\d+\.?\d*')#下載次數(shù) 被引次數(shù)num = driver.find_elements_by_xpath("//span[@class='count']")#獲取當(dāng)前窗口句柄 now_handle = driver.current_window_handle#獲取內(nèi)容i = 0for tag in content:print tag.textprint abstracts[i].textprint other[i].get_attribute("title")number = mode.findall(other[i].text)print number[0] #年份number = mode.findall(num[i].text)if len(number)==1: #由于存在數(shù)字確實(shí) 如(100) ()print number[0]elif len(number)==2:print number[0],number[1]print ''i = i + 1tag.click()time.sleep(2)#跳轉(zhuǎn) 獲取所有窗口句柄 all_handles = driver.window_handles #彈出兩個界面,跳轉(zhuǎn)到不是主窗體界面 for handle in all_handles: if handle!=now_handle: #輸出待選擇的窗口句柄 print handle driver.switch_to_window(handle) time.sleep(1) print u'彈出界面信息' print driver.current_url print driver.title #獲取登錄連接信息 elem_sub = driver.find_element_by_xpath("//div[@class='summary pad10']") print u"作者", elem_sub.text print '' #關(guān)閉當(dāng)前窗口 driver.close() #輸出主窗口句柄 print now_handle driver.switch_to_window(now_handle) #返回主窗口 開始下一個跳轉(zhuǎn) 但部分網(wǎng)站還是出現(xiàn)無法訪問的問題,如下所示:


最后作者擬爬取萬方數(shù)據(jù)進(jìn)行分析。
最后希望文章對你有所幫助,如果錯誤或不足之處,請海涵~
(By:Eastmount 2017-11-17 深夜12點(diǎn)??http://blog.csdn.net/eastmount/?)



與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的[python爬虫] BeautifulSoup和Selenium简单爬取知网信息测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。