Python实现的爬取百度文库功能示例
程序员文章站
2023-11-25 17:08:52
本文实例讲述了python实现的爬取百度文库功能。分享给大家供大家参考,具体如下:
# -*- coding: utf-8 -*-
from selenium...
本文实例讲述了python实现的爬取百度文库功能。分享给大家供大家参考,具体如下:
# -*- coding: utf-8 -*- from selenium import webdriver from bs4 import beautifulsoup from docx import document from docx.enum.text import wd_align_paragraph# 用来居中显示标题 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' #用来保存文档 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): # 得到标题 title_result = soup1.find('div', attrs={'class': 'doc-title'}) doc_title = title_result.get_text() # 得到文档标题 # 拖动滚动条 init_page = driver.find_element_by_xpath( "//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( "//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( "//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: #结束条件 print("找不到元素") stop_condition = true #next_page.send_keys(keys.enter) # 遍历所有的txt标签标定的文档,将其空格删除,然后进行保存 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) # 得到正文内容 sleep(2) # 防止页面加载过慢 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) # 处理字符编码问题 t_title = doc_title.split()[0] #print(t_title) #document.save('2.docx') document.save('百度文库-%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()
更多关于python相关内容可查看本站专题:《python socket编程技巧总结》、《python正则表达式用法总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》、《python入门与进阶经典教程》及《python文件与目录操作技巧汇总》
希望本文所述对大家python程序设计有所帮助。