Skip to content

Instantly share code, notes, and snippets.

@justinfay
Created May 16, 2012 22:41
Show Gist options
  • Save justinfay/2714629 to your computer and use it in GitHub Desktop.
Save justinfay/2714629 to your computer and use it in GitHub Desktop.
A xpath response hook for requests using lxml
from lxml import etree
def xpath(response):
response.xpath = etree.HTML(response.content).xpath
return response
if __name__ == "__main__":
import requests
r = requests.get('http://example.com', hooks=dict(response=xpath))
r.xpath('//h1/text()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment