user3435407
user3435407

Reputation: 1067

How to stop Scrapy Selector wrap an xml with html?

I do this:

xmlstr="<root><first>info</first></root>"

res = Selector(text=xmlstr).xpath('.').getall()
print(res)

The output is:

['<html><body><root><first>info</first></root></body></html>']

How can I stop Selector wrapping the xml with html and body? Thanks

Upvotes: 1

Views: 45

Answers (1)

stranac
stranac

Reputation: 28236

scrapy.Selector assumes html, but takes a type argument to change that.

type defines the selector type, it can be "html", "xml" or None (default).

If type is None, the selector automatically chooses the best type based on response type (see below), or defaults to "html" in case it is used together with text.

So, to make an xml selector, simply use Selector(text=xmlstr, type='xml')

Upvotes: 3

Related Questions