Advertisement
fixitsammie

Untitled

Sep 27th, 2022
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import requests
  2. import datetime
  3. from requests_html import HTML
  4.  
  5. now = datetime.datetime.now()
  6. year = now.year
  7.  
  8.  
  9. url = 'https://www.boxofficemojo.com/year/world/'
  10. """
  11. url,filename and save are parameters
  12. filename has been initialized as box.html
  13. while save has been initialized as False
  14. If you don't pass new values these will be used
  15. """
  16. def url_to_text(url,filename="box.html",save=False):
  17.     r = requests.get(url)
  18.     if r.status_code == 200:
  19.         html_text = r.text
  20.  
  21.         #here the program checks if save equal True. if save is false it exits
  22.         if save:
  23.             with open(f"box-{year}.html" , 'w',encoding="utf-8") as f:
  24.                 f.write(html_text)
  25.                 return html_text
  26.         return "" #code gets here if save =False
  27.  
  28.  
  29. html_text = url_to_text(url=url,save=True)
  30. print(type(html_text))
  31. r_html = HTML(html=html_text)
  32. print(r_html.find('a'))
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement