python - lxml: Element is not a child of this node -


i'm trying change value of title within following html document:

<html lang="en"> <head>   <meta charset="utf-8">   <title id="title"></title>   <base href="/">   <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body>   <app-root></app-root> </body> </html> 

i wrote following python script uses lxml, in order accomplish task:

from lxml.html import fromstring, tostring lxml.html import builder e  html = fromstring(open('./index.html').read()) html.replace(html.get_element_by_id('title'), e.title('test')) 

but after running script, following error:

valueerror: element not child of node.

what's supposed cause error? thank you.

the 'title' tag child of 'head' node. in code use replace on 'html' node, has no 'title' elements (not directly), hence valueerror.

you can desired results if use replace on 'head' node.

html.find('head').replace(html.get_element_by_id('title'), e.title('test')) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -