Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Unicode strings with encoding declaration are not supported."

ValueError: Unicode strings with encoding declaration are not supported. 
Please use bytes input or XML fragments without declaration.

When I try parsing this site doesn't work.

When I Try serialized this pagetext I have a error TypeError: Type 'str' cannot be serialized

from lxml import html

source = 'http://games.chruker.dk/eve_online/item.php?type_id=814'
path = '//*[@id="top"]/table[1]/tbody/tr[1]/td[3]/table'

page = requests.get(source)
pagetext = page.text

parser = html.fromstring(pagetext)

result = parser.xpath(path)
print(result)


I expect a table Requirements like in site: http://games.chruker.dk/eve_online/item.php?type_id=814

like image 300
Cre3d Avatar asked Sep 07 '19 11:09

Cre3d


2 Answers

Try this:

parser = html.fromstring(bytes(pagetext, encoding='utf8'))
like image 169
Sagar Gupta Avatar answered Nov 12 '22 15:11

Sagar Gupta


The parse function the API provides allows you to directly pass in a URL like you have in your source variable:

from lxml import html

source = 'http://games.chruker.dk/eve_online/item.php?type_id=814'
path = '//*[@id="top"]/table[1]/tbody/tr[1]/td[3]/table'

tree = html.parse(source)

result = tree.xpath(path)

print(result) 
like image 42
Martin Honnen Avatar answered Nov 12 '22 15:11

Martin Honnen