Skip to content

Instantly share code, notes, and snippets.

@victor-torres
Created November 29, 2018 15:05
Show Gist options
  • Save victor-torres/98757b1cd65a5af12e0f55afe664f7cb to your computer and use it in GitHub Desktop.
Save victor-torres/98757b1cd65a5af12e0f55afe664f7cb to your computer and use it in GitHub Desktop.
[Scrapy] Remove elements from the document based on a selector or list of selectors.
def remove_selector(selector):
"""
Remove elements from the document based on a selector or list of selectors.
:param selector: Selector object or Selector iterable.
"""
if not hasattr(selector, '__iter__'):
selector = [selector]
for sel in selector:
sel.root.getparent().remove(sel.root)
@farheenfathima96
Copy link

please guide for this error 'str' object has no attribute 'root'

@victor-torres
Copy link
Author

Hey @farheenfathima96,

  1. This pull request was merged into parsel (Scrapy dependency) so now you can run it directly from Scrapy/Parsel selectors 😃
  2. You're probably getting this error because you're trying to remove an element without parent or the document's root.
  3. If you're selecting a.my-link::text try using a.my-link instead – for example.

@farheenfathima96
Copy link

Hi victor-torres ,

Thank you.... It worked !!! 👍 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment