'python soup - scraped script returns unicode for characters, how to convert back?
in my script, i capture the contents of javascript via beautifoulsoup with following code:
product_data = soup.find_all('script')[2] # 3rd script tag
product_data = str(product_data).partition('return {')[-1]
product_data = '{' + product_data.split('}(')[0] + '}'
saleprice = price
try:
ean = str(re.findall('[0-9]{13}' , product_data)[0])
except:
ean = ""
try:
brand = str(re.findall('id:[0-9]{8},name:"(.+?)"', product_data))
except:
brand = ""
try:
description = str(re.findall('descriptionHtml:"(.+?)"', product_data)).replace('\\u003E', '').replace('\\u003Cbr', '').replace('\\u003Cli', '').replace('\\u003C', '').replace('\\u002Fli', '').replace('\\u002F', '').replace('\\\u002Fb', '').replace('\b', '').replace('\p', '')
except:
description = ""
what comes in return is, instead of the original characters, unicode for all these characters.
'description': "['\\\\u003Cp\\\\u003E\\\\u003Cb\\\\u003EIm System sind folgende komponenten verbaut:\\\\u003C\\\\u002Fb\\\\u003E\\\\u003C\\\\u002Fp\\\\u003E\\\\u003Cul\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EGehäuse:\\\\u003C\\\\u002Fb\\\\u003E Systemtreff Mini Tower Nero ST-401\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EProzessor: \\\\u003C\\\\u002Fb\\\\u003EIntel Core i5-10400F 6 x 2.9 GHz (bei Bedarf bis zu 4.3 GHz Turbotakt durch Intel Turbo-Boost Technik)\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EArbeitsspeicher:\\\\u003C\\\\u002Fb\\\\u003E 16 GB DDR4 2666 MHz \\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EMainboard:\\\\u003C\\\\u002Fb\\\\u003E Asus Prime B460M-A, Intel Sockel 1200(2 x PCIe 3.0(x16), 3x 3.0 (x1 mode), 1 x PS\\\\u002F2 keyboard\\\\u002Fmouse, 1 x HDMI, 1 x DisplayPort, 1 x DVI-D, 1 x LAN (RJ45), 2 x USB 3.2 Gen2, 2x USB 3.2 Gen1, 2 x M.2, 6 x SATA3) - max.128 GB DDR4 - 2933MHz\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003ENetzwerk:\\\\u003C\\\\u002Fb\\\\u003E Realtek® RTL8111H, 1 x Gigabit LAN Controller(s)\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003ESound:\\\\u003C\\\\u002Fb\\\\u003E Realtek® ALC887 7.1-Channel High Definition Audio CODEC\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EFestplatte:\\\\u003C\\\\u002Fb\\\\u003E 512GB M.2 SSD SATA III\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EGrafik:\\\\u003C\\\\u002Fb\\\\u003E NVIDIA GeForce GT 730 mit 2048 MB \\\\u002F 2GB RAM \\\\u003Cul\\\\u003E\\\\u003Cli\\\\u003ETechnik: ( GDDR3 \\\\u002F DirectX 11 \\\\u002F PCI Express 2.0 \\\\u002F ) \\\\u003C\\\\u002Fli\\\\u003E \\\\u003Cli\\\\u003EGeeignet für Heimvideos - Blu-ray FULL HD - Videobearbeitung \\\\u002F World of Warcraft, Spore oder Sims3, sowie die Anschlussmöglichkeiten von bis zu 2 Monitore\\\\u003C\\\\u002Fli\\\\u003E\\\\u003C\\\\u002Ful\\\\u003E\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003ENetzteil:\\\\u003C\\\\u002Fb\\\\u003E 400-500Watt Marken Netzteil\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003ELaufwerk:\\\\u003C\\\\u002Fb\\\\u003E Kein Laufwerk verbaut\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003EBetriebssystem:\\\\u003C\\\\u002Fb\\\\u003E Windows 10 Pro\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cbr\\\\u003E\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003E\\\\u003Cb\\\\u003ESKU:\\\\u003C\\\\u002Fb\\\\u003E 20192420\\\\u003C\\\\u002Fli\\\\u003E\\\\u003Cli\\\\u003EMarkennamen - Markenlogos sind registrierte Handelsmarken, deren Nutzung hier nur zur Produktbeschreibung eingesetzt werden - das Eigentumsrecht liegt beim jeweiligen Markeninhaber.\\\\u003C\\\\u002Fli\\\\u003E\\\\u003C\\\\u002Ful\\\\u003E']"
my current approach was to remove all these with regex to ectract the raw text, however, this is ugly and doesnt work with every result identically, as there are different unicode characters in every scraped item.
is there a way to "convert it back"?
(sample of scraped product page: https://www.kaufland.de/product/358005366)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
