在Python中使用Selenium在Firefox上保存网页 [英] Using Selenium in Python to save a webpage on Firefox

查看:335
本文介绍了在Python中使用Selenium在Firefox上保存网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python中的SeleniumMacOS Firefox上保存网页.

I am trying to use Selenium in Python to save webpages on MacOS Firefox.

到目前为止,我已经设法单击COMMAND + S弹出SAVE AS window.但是,

So far, I have managed to click COMMAND + S to pop up the SAVE AS window. However,

我不知道该怎么做:

  1. 更改文件目录,
  2. 更改名称 文件和
  3. 单击另存为"按钮.
  1. change the directory of the file,
  2. change the name of the file, and
  3. click the SAVE AS button.

有人可以帮忙吗?

下面是我用来单击COMMAND + S的代码:

Below is the code I have use to click COMMAND + S:

ActionChains(browser).key_down(Keys.COMMAND).send_keys("s").key_up(Keys.COMMAND).perform()

此外,我使用此方法的原因是当我:-

Besides, the reason for me to use this method is that I encounter Unicode Encode Error when I :-

  1. 将page_source写入html文件,然后
  2. 将报废的信息存储到一个csv文件中.

写入html文件:

file_object = open(completeName, "w")
html = browser.page_source
file_object.write(html)
file_object.close() 

写入一个csv文件:

csv_file_write.writerow(to_write)

错误:

UnicodeEncodeError:'ascii'编解码器无法在其中编码字符u'\ xf8' 位置1:序号不在范围内(128)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position 1: ordinal not in range(128)

推荐答案

with open('page.html', 'w') as f:
    f.write(driver.page_source)

这篇关于在Python中使用Selenium在Firefox上保存网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆