Writing to JSON - Converting \u00a3 to £

Viewed 1599

I am using Selenium and python to scrape a website. I am scraping some '£' Characters, however I am getting this instead: \u00a3, when writing to JSON (they appear as '£' with I print them to terminal).

I understand they are Unicode and I need them in UTF8 (?). I've tried a few things I've found on SO and haven't had much success.

I have tried .replace (.replace('\u00a3', '£') - However I'm not having much success.

How do I get the characters to look like '£' instead of \u00a3?

This is the line that's printing incorrectly. Let me know if you want to see my entire code.

price = page.find_element_by_class_name('header_tags').text
3 Answers

If you're using json.dump() or json.dumps(), try setting ensure_ascii=False

you can encode the string like below

s = 'This is a Pound sign \u00a3'
s.encode('utf8')
print(s)

Output

This is a Pound sign £

You need to call text("utf-8") while printing as follows:

print(page.find_element_by_class_name('header_tags').text("utf-8"))

But this issue can occur at some lines as well. So as per best practices start the Python file with the line:

# -*- coding: UTF-8 -*-

An example:

from selenium import webdriver
# other lines of code
price = page.find_element_by_class_name('header_tags').text
Related