Advertisement
jarekmor

pyscript_scrape

Dec 29th, 2022
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from requests_html import HTMLSession
  4.  
  5. @service
  6. def scrape_web(url=None, entity_id=None):
  7.     def sesja(url):
  8.         session = HTMLSession()
  9.         r = task.executor(session.get, url)
  10.        
  11.         #r.html.render()                         # to nie działa
  12.        
  13.         title = r.html.find("h1", first=True)
  14.         return title
  15.        
  16.     a = sesja(url)
  17.     log.info(a.text)
  18.    
  19.     state.set(entity_id, value=a.text)
  20.  
  21.  
  22. 2022-12-29 18:52:54.680 ERROR (MainThread) [custom_components.pyscript.file.test.scrape_web] Exception in <file.test.scrape_web> line 12:
  23.             r.html.render()                         # to nie działa
  24.             ^
  25. RuntimeError: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement