Skip to content

Instantly share code, notes, and snippets.

@schlarpc
Last active October 16, 2021 00:00
Show Gist options
  • Save schlarpc/6e0119ab5804ace6a90204137541da8e to your computer and use it in GitHub Desktop.
Save schlarpc/6e0119ab5804ace6a90204137541da8e to your computer and use it in GitHub Desktop.
import requests
import requests.models
import lxml.html
import cached_property
class LxmlResponse(requests.models.Response):
@classmethod
def use_in_session(cls, session=None):
if not session:
session = requests.Session()
session.hooks['response'].append(cls._hook_response)
return session
@classmethod
def _hook_response(cls, response, *args, **kwargs):
response.__class__ = cls
return response
@cached_property.cached_property
def document(self):
return lxml.html.fromstring(self.content, base_url=self.url)
def find(self, selector):
for result in self.find_all(selector):
return result
return None
def find_all(self, selector):
return self.document.cssselect(selector)
session = LxmlResponse.use_in_session()
print(session.get('http://example.com').find('a').get('href'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment