Python selenium如何打包静态网页并下载
需求:单纯的将page.source写入文件的方式,会导致一些图片无法显示,对于google浏览器,直接将页面打包下载成一个mhtml格式的文件,则可以进行离线下载。对应pythonselenium微信公众号历史文章随手一点就返回首页?郁闷之下只好将他们都下载下来。:https://www.nhooo.com/article/193111.htm
遇到的问题:
1、单纯使用webdriver.ActionChains无法完成下载动作,未能操作windows窗口。
2、没有找到相关能直接下载.mhtml的命名接口。
3、pywin32置顶窗口的使用不顺利。
解决思路:
1、使用selnium打开浏览器,不要操作,让其保持置顶
2、使用pyautogui、pyperclip操作键盘、鼠标、剪切板进行下载文件。
准备材料:
需要将自动化操作的一些图片截取下来,以作为后期图片匹配使用。
实现:
1、打开爬取好的链接,遍历所有需要下载的页面
#读取文件
filename=r'data/01爬取微信公众号历史文章/urls二律背反的一灯如豆-out.xlsx'
df=pd.read_excel(filename,dtype=object)
df=df.reindex(columns=['日期','标题','原创','地址','完成情况','储存地址'])
#df=df.head(5)
dfsel=(df['标题']!='随文')&(df['完成情况']!=1)
save_folder=r"I:\code\python\data\01爬取微信公众号历史文章\01二律背反的一灯如豆"+"\\"
#设置保存格式为mhtml,减少要操作文件保存下拉框的情况
options=webdriver.ChromeOptions()
options.add_argument('--save-page-as-mhtml')
#启动浏览器
driver=webdriver.Chrome(options=options)
wait=WebDriverWait(driver,10)
df.loc[dfsel,"完成情况"],df.loc[dfsel,"储存地址"]=zip(*df[dfsel].apply(download_mhtml_with_not_check,axis=1,args=(driver,wait)))
2、编写相关下载页面函数
#在timeout秒内,返回中心值,间隔时长time_setp #封装一个pyautogui限时查找函数 # deffinde_gui_element(png,timeout=5,time_setp=0.2): i=1 iftimeout<=0:timeout=5 iftime_setp<=0:time_setp=0.2 whileTrue: ifi>timeout/time_setp:returnNone center=pyautogui.locateCenterOnScreen(png,grayscale=False,confidence=0.9) ifcenter==None: time.sleep(0.2) else: returncenter i=i+1
主要自动化操作代码:
defdownload_mhtml_with_not_check(x,driver,wait):
name=''
try:
url=str(x['地址'])
driver.get(url)
#获取浏览器标题,用于检测是否是置顶页
wait.until(EC.presence_of_element_located((By.XPATH,'//h2[@id="activity-name"]')))
title=driver.find_element_by_xpath('//h2[@id="activity-name"]').text
print('no:',x.name,'url:',url,'title:',title)
wait.until(EC.presence_of_element_located((By.XPATH,'//div[@id="page-content"]')))
#进入下载
pyautogui.hotkey('ctrl','s')
#等待一下对话框弹出
time.sleep(1)
bt=finde_gui_element(r'data\png\save.png')#查找保存按键
ifbt==None:
return(0,'')
else:
#根据标题组合成具体路径
name=save_folder+''+title+'.mhtml'
#print(name)
pyperclip.copy(name)
pyautogui.hotkey('ctrl','v')
time.sleep(0.1)
pyautogui.hotkey('Enter')
#检查是否弹出另存为
bt=finde_gui_element(r'data\png\confirmsaveas.png',timeout=0.5)
ifbt!=None:
#说明出现重复明明,点击覆盖
pyautogui.hotkey('Tab')
pyautogui.hotkey('Enter')
return(1,name)
bt=finde_gui_element(r'data\png\cancle.png',timeout=0.5)
ifbt!=None:
#还爱,说明出现了一些异常
pyautogui.hotkey('esc')
pyautogui.hotkey('esc')
pyautogui.leftClick(bt)
return(-1,name)
#加多一个esc防止出现窗口还在
pyautogui.hotkey('esc')
exceptExceptionase:
print(str(e))
return(-2,name)
return(1,name)
最后写入excel:
通过vba代码,将单元格地址添加上超链接:
OptionExplicit Subadd_hype() DimwsAsWorksheet,arrAsVariant,iAsLong Setws=ThisWorkbook.Worksheets(1) arr=ws.UsedRange.Value ws.Cells.Hyperlinks.Delete Fori=2ToUBound(arr) IfCStr(arr(i,2))="随文"Then Else IfCStr(arr(i,5))="1"Then ws.Hyperlinks.AddAnchor:=ws.Cells(i,6),Address:=CStr(arr(i,6)) EndIf EndIf Nexti EndSub
完成。
不足之处:
1、通过autogui操作,难免会遇到弹窗的情况,需要增加活动窗体置顶,但是一直没有找到有效的方法。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。