首先:
感谢CSDN平台、python小课的老师、还有@Jack-Cui的文章、@puck441的回复,感谢他们让我有了这样的一次体验。
其次:
奉上代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time
from docx import Document
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"')
#模拟手机登录,因为手机和PC打开后客户端显示是不一样的
driver = webdriver.Chrome(chrome_options=options) #打开chrome浏览器
driver.get("https://wenku.baidu.com/view/b9a940d65901020206409c5b.html?from=search") #文库网址
time.sleep(3) #缓冲需要
VIP_btn=driver.find_element_by_xpath('//div[@class="foldpagewg-text-con"]')
driver.execute_script('arguments[0].scrollIntoView();',VIP_btn) #翻页,翻到继续阅读
page=driver.find_element_by_xpath('//div[@class="foldpagewg-icon"]').click() #找到字段(继续阅读这里)
time.sleep(1) #加1s延迟,为了安全
html = driver.page_source #获取原代码
bf1 = BeautifulSoup(html, 'html.parser')#解析源代码
result = bf1.select('.txt')#定位元素txt
document = Document() #接入word
for re in result:
q=re.get_text()
p = document.add_paragraph(q)
document.save('baidu.docx')
最后:
对代码中不明白的同仁,欢迎联系我。