am_dot_com

FP 2022-12-06

Dec 6th, 2022 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. # w2.py
  2. # http://arturmarques.com/edu/fp
  3. # https://arturmarques.com/edu/fp
  4.  
  5. import certifi
  6. from urllib.request import urlopen
  7. from http.client import HTTPResponse
  8. import ssl # secure sockets layer
  9.  
  10. ondeEstaOMeuFicheiroDeCertificados =\
  11. certifi.where()
  12.  
  13. sslContext =\
  14. ssl.create_default_context(
  15. cafile=ondeEstaOMeuFicheiroDeCertificados
  16. )
  17.  
  18. print(ondeEstaOMeuFicheiroDeCertificados)
  19.  
  20. URL = "https://dn.pt/"
  21. resposta:HTTPResponse = urlopen(
  22. url = URL, #Uniform Resource Locator
  23. context=sslContext
  24. )
  25. resultadoEmBytesDoConsumo = resposta.read()
  26. resultadoEnquantoFraseUTF8 =\
  27. str(
  28. resultadoEmBytesDoConsumo,
  29. "UTF-8"
  30. )
  31. #print(resultadoEmBytesDoConsumo) #mostra bytes
  32. print (resultadoEnquantoFraseUTF8)
  33.  
  34. """
  35. As hiperligações em HTML são expressas pela marca "a"
  36. a - anchor
  37. a forma geral de uma âncora
  38. <a href="https://dn.pt/noticia1.html" atributo2="v2">
  39. Hoje aconteceu o evento 1
  40. </a>
  41. """
  42.  
  43. import bs4
  44. oBS4 =\
  45. bs4.BeautifulSoup(
  46. resultadoEnquantoFraseUTF8,
  47. "html5lib"
  48. )
  49. theAnchors = oBS4.find_all("a")
  50. for a in theAnchors:
  51. if ("href" in a.attrs.keys()):
  52. print(a.attrs["href"])
Advertisement
Add Comment
Please, Sign In to add comment