Advertisement
dimkiriakos

pyppeteer

Oct 20th, 2021
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import asyncio
  2. from pyppeteer import launch
  3.  
  4.  
  5. async def save(content, extension):
  6.     file = open(f"result.{extension}", 'w+')
  7.     file.write(content)
  8.     file.close()
  9.  
  10. async def main(contentType):
  11.     browser = await launch()
  12.     page = await browser.newPage()
  13.     await page.goto('https://example.com')
  14.     await page.screenshot({'path': 'example.png'})
  15.     if contentType == 'html':
  16.         # 1 get the html contnet
  17.         content = await page.evaluate('document.body.innerHTML', force_expr=True)
  18.         print(content)
  19.         # 2 get the text
  20.     elif contentType == 'txt':
  21.         content = await page.evaluate('document.body.textContent', force_expr=True)
  22.         print(content)
  23.     await save(content, 'html')
  24.     await browser.close()
  25.  
  26.  
  27. contentType = input('What do you want to save: html or txt: ')
  28.  
  29.  
  30. asyncio.get_event_loop().run_until_complete(main(contentType))
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement