Clarification on deprecation - XML scrapy.selector.XmlXPathSelector

255 views
Skip to first unread message

Sayth Renshaw

unread,
Jan 23, 2016, 10:35:42 PM1/23/16
to scrapy-users
Hi

Just putting together my first xml parser in scrapy and while it works i noted a deprecation in the logs and wanted to know what I should be doing moving forward. Below is my code this is the warning.


2016-01-24 14:32:04 [py.warnings] WARNING: /home/sayth/.virtualenvs/scrapy_xml/local/lib/python2.7/site-packages/scrapy/selector/unified.py:108: ScrapyDeprecationWarning: scrapy.selector.XmlXPathSelector is deprecated, instantiate scrapy.Selector instead.
  for x in result]

# -*- coding: utf-8 -*-
import scrapy
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
from scrapy.selector import XmlXPathSelector

class MyxmlSpider(scrapy.Spider):
    name = "myxml"

    start_urls = (
        ["file:///home/sayth/Downloads/20160123RAND0.xml"]
    )

    def parse(self, response):
        xpath = XmlXPathSelector(response)
        for ids in xpath.select('.//race/@id'):
            print(ids.extract())
Thanks

Sayth

Sayth Renshaw

unread,
Jan 23, 2016, 10:54:51 PM1/23/16
to scrapy-users
Sorry is it this sel = Selector(xml_response)

Only issue is that it returns xml_response error.

  File "/home/sayth/.virtualenvs/scrapy_xml/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 588, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/sayth/Projects/conv_xml/conv_xml/spiders/myxml.py", line 14, in parse
    sel = Selector(xml_response)
NameError: global name 'xml_response' is not defined

Valdir Stumm Junior

unread,
Jan 25, 2016, 6:28:44 AM1/25/16
to scrapy...@googlegroups.com
Hey!

Since Scrapy 0.24 you don't need to instantiate selectors manually to deal with the response. You can just call one of the shortcut methods: response.xpath() or response.css().

def parse(self, response):
    for ids in response.xpath('.//race/@id'):
        ...

    
    

--
You received this message because you are subscribed to the Google Groups "scrapy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scrapy-users...@googlegroups.com.
To post to this group, send email to scrapy...@googlegroups.com.
Visit this group at https://groups.google.com/group/scrapy-users.
For more options, visit https://groups.google.com/d/optout.



--
Scrapinghub

Valdir Stumm Junior 
Developer Evangelist, Scrapinghub 

Skypestummjr
TwitterGithub
TwitterLinkedInGithub

We turn web content into structured data. Lead maintainers of Scrapy.

Sayth Renshaw

unread,
Jan 25, 2016, 6:59:30 AM1/25/16
to scrapy-users

Thanks

Simpler, more to the point now I want a response from xpath, it reads what it does, makes sense.

thanks again 

sayth
Reply all
Reply to author
Forward
0 new messages