2

I am trying to save source code with Selenium into .txt, but the .txt file stays empty.

When I tryed to print the source code with command:

htmlcode = driver.page_source
(driver.page_source).encode('utf-8'))
print(htmlcode)

It will print the source code but then it kills the script with error:

File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u20ac' in position 16329: character maps to <undefined>
2
  • Can you post the code that saves to file ? Commented Feb 10, 2017 at 9:40
  • I used the script below, but this wont work because of the encode error i gues. site=browser.open(link) fileobjj = open("output.txt","wb") fileobjj.write(site.read()) fileobjj.close() Commented Feb 10, 2017 at 12:35

1 Answer 1

2

Problem solved! After 3 hours searching ':-)

html = driver.page_source
f = open('savepage.html', 'w')
f.write(html.encode('utf-8'))
f.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.