Python實(shí)現(xiàn)的爬取百度文庫(kù)功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)的爬取百度文庫(kù)功能。分享給大家供大家參考,具體如下:
# -*- coding: utf-8 -*-
from selenium import webdriver
from bs4 import BeautifulSoup
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH# 用來(lái)居中顯示標(biāo)題
from time import sleep
from selenium.webdriver.common.keys import Keys
# 瀏覽器安裝路徑
#BROWSER_PATH=\'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe'
#目的URL
DEST_URL='https://wenku.baidu.com/view/aa31a84bcf84b9d528ea7a2c.html'
#用來(lái)保存文檔
doc_title = ''
doc_content_list = []
def find_doc(driver, init=True):
global doc_content_list
global doc_title
stop_condition = False
html = driver.page_source
soup1 = BeautifulSoup(html, 'lxml')
if (init is True): # 得到標(biāo)題
title_result = soup1.find('div', attrs={'class': 'doc-title'})
doc_title = title_result.get_text() # 得到文檔標(biāo)題
# 拖動(dòng)滾動(dòng)條
init_page = driver.find_element_by_xpath( "http://div[@class='foldpagewg-text-con']")
print(type(init_page), init_page)
driver.execute_script('arguments[0].scrollIntoView();', init_page)
init_page.click()
init = False
else:
try:
page = driver.find_element_by_xpath( "http://div[@class='pagerwg-schedule']")
#print(type(next_page), next_page)
next_page = driver.find_element_by_class_name("pagerwg-button")
station = driver.find_element_by_xpath( "http://div[@class='bottombarwg-root border-none']")
driver.execute_script('arguments[0].scrollIntoView(false);', station)
#js.executeScript("arguments[0].click();",next_page);
#sleep(5)
'''js = "window.scrollTo(508,600)"
driver.execute_script(js)'''
next_page.click()
except:
#結(jié)束條件
print("找不到元素")
stop_condition = True
#next_page.send_keys(Keys.ENTER)
# 遍歷所有的txt標(biāo)簽標(biāo)定的文檔,將其空格刪除,然后進(jìn)行保存
content_result = soup1.find_all('p', attrs={'class': 'txt'})
for each in content_result:
each_text = each.get_text()
if ' ' in each_text:
text = each_text.replace(' ', '')
else:
text = each_text
# print(each_text)
doc_content_list.append(text)
# 得到正文內(nèi)容
sleep(2) # 防止頁(yè)面加載過(guò)慢
if stop_condition is False:
doc_title, doc_content_list = find_doc(driver, init)
return doc_title, doc_content_list
def save(doc_title, doc_content_list):
document = Document()
heading = document.add_heading(doc_title, 0)
heading.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中顯示
for each in doc_content_list:
document.add_paragraph(each)
# 處理字符編碼問(wèn)題
t_title = doc_title.split()[0]
#print(t_title)
#document.save('2.docx')
document.save('百度文庫(kù)-%s.docx'% t_title)
print("\n\nCompleted: %s.docx, to read." % t_title)
driver.quit()
if __name__ == '__main__':
options = webdriver.ChromeOptions()
options.add_argument('user-agent="Mozilla/5.0 (Linux; Android 4.0.4; \ Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) \ Chrome/18.0.1025.133 Mobile Safari/535.19"')
#driver = webdriver.Chrome(BROWSER_PATH, chrome_options=options)
driver = webdriver.Chrome(chrome_options=options)
driver.get(DEST_URL)
#JavascriptExecutor js = (JavascriptExecutor) driver;
print("**********START**********")
title, content = find_doc(driver, True)
save(title, content)
driver.quit()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python 爬取百度文庫(kù)并下載(免費(fèi)文章限定)
- 用python下載百度文庫(kù)的代碼
- python 爬蟲(chóng)如何實(shí)現(xiàn)百度翻譯
- 詳解用Python爬蟲(chóng)獲取百度企業(yè)信用中企業(yè)基本信息
- Python爬蟲(chóng)爬取百度搜索內(nèi)容代碼實(shí)例
- Python爬蟲(chóng)實(shí)現(xiàn)百度翻譯功能過(guò)程詳解
- python 爬蟲(chóng)百度地圖的信息界面的實(shí)現(xiàn)方法
- python爬蟲(chóng)之爬取百度音樂(lè)的實(shí)現(xiàn)方法
- Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能實(shí)例
- python爬蟲(chóng)獲取百度首頁(yè)內(nèi)容教學(xué)
- Python爬蟲(chóng)實(shí)現(xiàn)百度圖片自動(dòng)下載
- Python爬蟲(chóng)實(shí)例_利用百度地圖API批量獲取城市所有的POI點(diǎn)
- python實(shí)現(xiàn)百度文庫(kù)自動(dòng)化爬取
相關(guān)文章
基于Python實(shí)現(xiàn)模擬三體運(yùn)動(dòng)的示例代碼
此前所做的一切三體和太陽(yáng)系的動(dòng)畫,都是基于牛頓力學(xué)的,而且直接對(duì)微分進(jìn)行差分化,從而精度非常感人,用不了幾年就得撞一起去。所以本文來(lái)用Python重新模擬一下三體運(yùn)動(dòng),感興趣的可以了解一下2023-03-03
在python中用print()輸出多個(gè)格式化參數(shù)的方法
今天小編就為大家分享一篇在python中用print()輸出多個(gè)格式化參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python?tkinter中四個(gè)常用按鈕的用法總結(jié)
tkinter中有四個(gè)控件被冠以Button之名,分別是:Button,?Checkbutton,?Radiobutton,?Menubutton,下面小編就來(lái)和大家聊聊它們的具體用法,感興趣的可以學(xué)習(xí)一下2023-09-09
Python利用字典將兩個(gè)通訊錄文本合并為一個(gè)文本實(shí)例
這篇文章主要介紹了Python利用字典將兩個(gè)通訊錄文本合并為一個(gè)文本實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

