python+selenium百度文库ppt(爬虫)

#导入的包
import os.path
import time
from selenium import webdriver
import requests
from selenium.webdriver.support.select import Select

url="https://wenku.baidu.com/view/bf2978c1acf8941ea76e58fafab069dc50224736.html"
#第一部分:手机模式打开一个电脑浏览器
options=webdriver.ChromeOptions()                                    #配置chrome启动时属性的类
mobile_emulation={"deviceName":"iPhone 6"}                           #手机模式打开浏览器,手机类型:iPhone 6
options.add_experimental_option("mobileEmulation",mobile_emulation)  #将mobile_emulation 添加到options中,这样浏览器就是通过手机模式打开
web = webdriver.Chrome(executable_path='C:\Program Files (x86)\Google\Chrome/Application\chromedriver.exe', chrome_options=options)
web.get(url)
time.sleep(2)

#第二部分:将所有隐藏的ppt图片展示出来
def click_ele(click_xpath):
    # 单击指定控件
    click_ele = web.find_elements_by_xpath(click_xpath)
    if click_ele:
        click_ele[0].location_once_scrolled_into_view                               # 滚动到控件位置
        web.execute_script('arguments[0].click()', click_ele[0])                    # 单击控件,即使控件被遮挡,同样可以单击
 #点击继续阅读
xpath_continue_read_button = "//div[@class='foldpagewg-icon']"                      #获取继续阅读得到xpath
click_ele(xpath_continue_read_button)                                               #调用click_ele()
xpath_next_content_button = "//div[@class='btn-wrap']/div[@class='btn-cancel']"     #获取下一页的xpath
click_ele(xpath_next_content_button)                                                #调用click_ele()
click_count = 0
while True:
    # 如果到了最后一页就跳出循环
    if web.find_elements_by_xpath(
            "//div[@class='pagerwg-loadSucc hide']") or web.find_elements_by_xpath(
            "//div[@class='pagerwg-button' and @style='display: none;']"):
        break
    # 点击加载更多
    xpath_loading_more_button = "//span[@class='pagerwg-arrow-lower']"
    click_ele(xpath_loading_more_button)
    click_count += 1
    print("第{}次点击加载更多!".format(click_count))
    # 等待一秒,等浏览器加载
    time.sleep(2)
click_ele('//*[@id="wui-messagebox-cancel-1"]')
time.sleep(1)
#图片元素的定位及获取
li_list=web.find_elements_by_class_name('retype-page')
img_url=[]
for i in li_list:
    h2=i.find_element_by_class_name('pic')
    time.sleep(3)
    h3=h2.find_element_by_tag_name('img')
    time.sleep(1)
    img=h3.get_attribute('src')
    img_url.append(img)
#创建文件夹进行保存
path='E://爬虫ppt图片//picture'
if not os.path.exists(path):        #查找E盘是否有存储的文件夹,没有则创建一个
    os.makedirs(path)
#解析图片url,并保存到已创建的文件夹中
x=1   
for g in range(len(img_url)):
    r=requests.get(img_url[g])
    path = 'E://爬虫ppt图片//picture//%d.jpg' % x
    print('正在爬取' + img)
    with open(path, "wb") as f:
        f.write(r.content)
        time.sleep(2)
        f.close()
        print('爬取成功')
        x+=1 

#参考:https://github.com/siyangbing/baiduwenku

代码有待改进,希望大家多多指正

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值