这边想通过代码获取百度文库中PPT内容并保存,代码是从网上找的,更改过后,两个版本。一个一直在获取,但获取不出来,一个报错
Warning (from warnings module):
File "C:\Users\86185\Desktop\大作业\百度文库\1.1.py", line 74
self.brower = webdriver.Chrome(chrome_options=options)
DeprecationWarning: use options instead of chrome_options
Traceback (most recent call last):
File "C:\Users\86185\Desktop\大作业\百度文库\1.1.py", line 283, in <module>
start_chrome.create_doc(ppt_dir_path, doc_dir_path)
File "C:\Users\86185\Desktop\大作业\百度文库\1.1.py", line 231, in create_doc
title = "".join(sel.xpath(xpath_title).extract()).strip()
AttributeError: 'str' object has no attribute 'xpath'
后面的报错不知道怎么更改?
该代码使用的是已经过时的webdriver模块,并且存在一些问题。建议使用更新的浏览器自动化工具,例如Selenium。以下是使用Selenium的示例代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
# 设置Chrome浏览器选项
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("disable-extensions")
# 创建浏览器对象
self.brower = webdriver.Chrome(chrome_options=options)
# 百度文库登录页面
url = "https://passport.baidu.com/v2/api/?getapi&class=1&index=3&name=login"
self.brower.get(url)
# 输入用户名和密码
self.brower.find_element_by_xpath("//input[@name='username']").send_keys("your username")
self.brower.find_element_by_xpath("//input[@name='password']").send_keys("your password")
# 提交表单
self.brower.find_element_by_xpath("//button[@type='submit']").click()
# 打开百度文库PPT页面
url = "https://wenku.baidu.com/view/9a13e2c45acfa1c7aa00ccff"
self.brower.get(url)
# 定义一个函数,下载PPT
def download_ppt(self, ppt_dir_path, doc_dir_path):
# 遍历页面中所有的PPT链接
for i in range(30):
# 获取链接列表
links = []
for j in range(1, 10):
url = f"https://wenku.baidu.com/view/page/{i*10000+j}"
self.brower.get(url)
# 获取页面中所有的链接
links += self.brower.find_elements_by_css_selector(".download-txt a")
# 遍历所有链接并下载PPT
for link in links:
file_name = link.get_attribute("href").split("/")[-1]
file_path = os.path.join(doc_dir_path, file_name)
with open(file_path, "wb") as f:
self.brower.implicitly_wait(10) # 等待页面加载完毕
f.write(self.brower.get_binary()) # 下载PPT文件
print(f"下载完成:{file_name}")
# 调用下载PPT函数,设置保存路径
start_chrome = webdriver.Chrome(chrome_options=options)
start_chrome.create_doc("C:/Users/86185/Desktop/大作业/百度文库", "C:/Users/86185/Desktop/大作业/百度文库/文档")
不知道你这个问题是否已经解决, 如果还没有解决的话:s = 0
for i in range(5):
while True:
try:
g = eval(input("请输入第{}门课的成绩:\n".format(i+1)))
if g>=0 and g<=100:
break
except:
continue
s = s + g # s += g
print('你的平均成绩为:{:.2f}!'.format(s/5))
首先需要了解一下百度文库中PPT内容的组成方式,PPT的每个页面都是一个图片,因此获取PPT内容需要先获取每一页的图片链接,再将图片下载保存。接下来给出一种可能的解决方案:
from selenium import webdriver
# 创建Selenium驱动,使用Chrome浏览器驱动
driver = webdriver.Chrome()
# 访问百度文库登录页面
driver.get("https://passport.baidu.com/v2/?login")
# 填写用户名和密码并点击登录按钮
username = driver.find_element_by_id("TANGRAM__PSP_4__userName")
username.send_keys("your_username")
password = driver.find_element_by_id("TANGRAM__PSP_4__password")
password.send_keys("your_password")
login_button = driver.find_element_by_id("TANGRAM__PSP_4__submit")
login_button.click()
# 访问指定PPT页面
driver.get("https://wenku.baidu.com/view/xxx.html")
# 获取PPT总页数并构造每一页的链接
total_page = int(driver.find_element_by_xpath("//div[@class='page-count']/span[2]/text()"))
page_links = []
for i in range(1, total_page+1):
page_link = "https://wenku.baidu.com/view/xxx.html?pn={}".format(i)
page_links.append(page_link)
import requests
from lxml import etree
# 构造请求头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
# 循环获取每一页的图片链接
image_links = []
for page_link in page_links:
# 发送GET请求并获取网页数据
response = requests.get(page_link, headers=headers)
# 解析出图片链接并添加到列表中
html = etree.HTML(response.text)
image_links += html.xpath("//div[@class='ppt-image-wrap']/img/@src")
# 循环下载每个图片并保存到本地文件中
for i, image_link in enumerate(image_links):
response = requests.get(image_link, headers=headers)
with open("page{}.jpg".format(i+1), "wb") as f:
f.write(response.content)
总结:以上是一种可能的解决方案,需要根据具体情况和需求进行修改和完善。需要注意的是,爬取别人的内容需要遵守法律法规和爬虫道德规范,不能用于商业用途和侵犯他人权益。