【发布时间】:2023-03-09 06:41:01
【问题描述】:
我正在尝试将页面源代码保存到一个文件中,这样我就不必在每次想要测试某些内容时都不断地重新运行我的代码。
我有:
html_source = driver.page_source
soup = BeautifulSoup(html_source, 'lxml') # added `lxml` only b/c I got a warning saying I should
soup = soup.prettify()
with open('pagesource.html', 'wb') as f_out:
f_out.write(soup)
我得到的错误是:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xab' in position 223871: ordinal not in range(128)
我也试过f_out.write(str(soup)),还是不行。
如何将内容写入文件?
【问题讨论】:
标签: python file selenium-webdriver io beautifulsoup