Python实现的爬取百度文库功能示例
作者:i_have_a_girlfriend 发布时间:2022-09-08 22:34:46
标签:Python,爬取,百度文库
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 代码: (使用os.listdir) import osdef ListFilesToTxt(dir,file,wildcard,recur
- 家里没网络,实在无聊,玩游戏都没味道,只好玩CSS。这个东西实在没什么技术含量在里面,纯属打发时间。简单说明:一张图片放在底层,
- 引入:通常,钓鱼网站本质是本质搭建一个跟正常网站一模一样的页面,用户在该页面上完成转账功能转账的请求确实是朝着正常网站的服务端提交,唯一不同
- 一、问题说明首先,运行下述代码,复现问题:# -*-coding:utf-8-*-import reimport requestsfrom
- ASP由于是一种古老的语言,它的一些功能对UTF-8支持非常差。比如,你想生成一个UTF-8格式的文件,使用常用的 scrīpting.Fi
- 前言mpvue 是美团开源的一套语法与vue.js一致的、快速开发小程序的前端框架,按官网说可以达到小程序与H5界面使用一套代码。使用此框架
- 本文实例讲述了Django rest framework工具包简单用法。分享给大家供大家参考,具体如下:Django rest framew
- 一、需求描述文本溢出省略,说实话这些年也实践过很多了,这次是针对富文本字符串,思量想去,也曾试图了解一些知名站点的实现方案,但结果不甚理想。
- 利用numpy、matplotlib、sympy绘制sigmoid、tanh、ReLU、leaky ReLU、softMax函数起因:深度学
- 字体的处理在网页设计中无论怎么强调也不为过,毕竟网页使用来传递信息的,而最经典最直接的信息传递方式就是
- 前言在使用vue.js开发前端项目时,再结合webpack搞起各种依赖、各种插件进行开发,无疑给前端开发带来了很多便捷,就在解决跨域这个问题
- 创建工程时,选择已存在的环境(不要选择虚拟环境,虽然安全,但很慢)添加环境在 anaconda3/bin/python3.6 即 d:/an
- 前言本文实现一个 Python 脚本,用来批量卸载模拟器或者实体机上面的 App 以及清除 LogCat 缓存。开发 Android 的朋友
- 前面介绍过vSQLAlchemy中的 Engine 和 Connection,这两个对象用在row SQL (原生的sql语句)上操作,而
- 前言在Django应用程序中发送电子邮件最常见的用例是密码重置、帐户激活和发送与您的应用程序相关的一般通知。下面来看看详细的介绍吧。配置Dj
- 我就废话不多说了,大家还是直接看代码吧!#加载keras模块from __future__ import print_functionimp
- 本文实例讲述了Python计算斗牛游戏概率算法。分享给大家供大家参考,具体如下:过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,
- 我就废话不多说了,大家还是直接看代码吧!import cv2# 读取图片并缩放方便显示img = cv2.imread('D:/6.
- 这篇文章收集了我在Python新手开发者写的代码中所见到的不规范但偶尔又很微妙的问题。本文的目的是为了帮助那些新手开发者渡过写出丑陋的Pyt
- 1. 基本介绍tensorflow设备内存管理模块实现了一个best-fit with coalescing算法(后文简称bfc算法)。bf