Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将driver.page_源写入二进制文件?_Python_Python 2.7_Selenium_Unicode - Fatal编程技术网

Python 如何将driver.page_源写入二进制文件?

Python 如何将driver.page_源写入二进制文件?,python,python-2.7,selenium,unicode,Python,Python 2.7,Selenium,Unicode,我正在使用selenium从网站下载xls。Selenium单击一个按钮,结果是driver.page\u source现在包含Excel电子表格 如何将driver.page_source的值提取到一个二进制文件中,我可以在Excel、libreoffice等中打开该文件 如果我只是尝试将其写入一个文件,就会遇到ascii编码等问题。我首先尝试将其转换为bytearray,但它似乎仍然需要某种编码,而且“utf-8”无法生成一个可用的excel文件。这可能不是最优雅的解决方案,但最终对我有效的

我正在使用selenium从网站下载xls。Selenium单击一个按钮,结果是
driver.page\u source
现在包含Excel电子表格

如何将driver.page_source的值提取到一个二进制文件中,我可以在Excel、libreoffice等中打开该文件


如果我只是尝试将其写入一个文件,就会遇到ascii编码等问题。我首先尝试将其转换为bytearray,但它似乎仍然需要某种编码,而且“utf-8”无法生成一个可用的excel文件。

这可能不是最优雅的解决方案,但最终对我有效的方法是简单地逐个写入每个字节,如下所示:

f = open('report.xls', 'wb')
for uchar in driver.page_source:
    f.write(bytearray([ord(uchar)]))
f.close()

这产生了一个工作的Excel文件,然后我可以在libreoffice等中打开该文件。

为什么要使用向下投票?