python爬取百度搜索pdf文档(Python实现的爬取百度文库功能示例)
类别:脚本大全 浏览量:2684
时间:2022-03-28 16:52:17 python爬取百度搜索pdf文档
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( 'li' , attrs = { 'class' : 'doc-title' }) doc_title = title_result.get_text() # 得到文档标题 # 拖动滚动条 init_page = driver.find_element_by_xpath( "//li[@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( "//li[@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( "//li[@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程序设计有所帮助。
您可能感兴趣
- 2021-10-23 10:04:24
- python语言的读取图像(详解python读取image)
- python程序开发过程(python调用外部程序的实操步骤)
- python3正则表达式详解(Python正则表达式和re库知识点总结)
- python 字典按key值排序(Python 按字典dict的键排序,并取出相应的键值放于list中的实例)
- python操作mysql连接池(详解Python连接MySQL数据库的多种方式)
- python 爬虫图形验证码(Python爬虫实现验证码登录代码实例)
- python json转换字符串(python3 json数据格式的转换dumps/loads的使用、dict to str/str to dict、json字符串/字典)
- python多线程多进程运行场景(Python多线程同步---文件读写控制方法)
- python3知识点汇总(Python3几个常见问题的处理方法)
- python自己做个定时器(对python周期性定时器的示例详解)
- python图片模板匹配(python实现简单图片物体标注工具)
- python实现七个基本算法(python实现维吉尼亚算法)
- python对列表排序(Python实现对特定列表进行从小到大排序操作示例)
- pythonhtml文件转换成pdf库(Python3转换html到pdf的不同解决方案)
- 如何用python爬取最新电影(详解Python爬取并下载《电影天堂》3千多部电影)
- 重温《蜗居》 宋思明选中海藻为红颜知己,纯属巧合,与爱无关(宋思明选中海藻为红颜知己)
- 越南旅游攻略(越南旅游攻略自由行)
- 成都旅游攻略(成都旅游攻略自由行最佳线路)
- 给儿童吃什么最好(给儿童吃什么最好消化)
- 杭州旅游攻略()
- 云南旅游攻略(云南旅游攻略5天攻略)
热门推荐
- sqlserver怎么写判断条件(SQL Server之SELECT INTO 和 INSERT INTO SELECT案例详解)
- 使用VS调试存储过程
- dedecms权限调整(dedecms当前位置导航去掉链接的实现方法)
- thinkphp框架实例(ThinkPHP框架整合微信支付之JSAPI模式图文详解)
- ftp文件管理工具(ftp服务器PDF文件在线查看的实现方法)
- 移动网站性能优化
- dedecms无缩略图怎么设置(dedecmsV5.7版 tag标签长度的修改方法详解)
- mysql日期与时间函数(MySQL日期函数与时间函数汇总MySQL 5.X)
- web前端开发怎么算乘法(如何计算Web动画帧率FPS)
- docker 构建整个环境(Docker如何快速搭建LNMP环境最新)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9