Guest User

Untitled

a guest
Mar 10th, 2023
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests, csv, re
  3. from datetime import datetime
  4. date = datetime.today().date()
  5. print(str(date))
  6. URL = "https://apewisdom.io/all-crypto/"
  7. page = requests.get(URL)
  8. soup = BeautifulSoup(page.content, "html.parser")
  9. criptos = soup.find_all("div", class_="company-name")
  10. criptos = str(criptos)
  11. criptos = criptos.replace('<div class="company-name">','|')
  12. criptos = criptos.replace('</div>,','|')
  13. criptos = criptos.replace('[','')
  14. criptos = criptos.replace('</div>]','')
  15. criptos = criptos.replace(' ','')
  16. criptoclean = criptos.split("|")
  17. print("###########")
  18. #clean empty data from the list
  19. criptoclean[:] = [x for x in criptoclean if x]
  20. print(criptoclean)
  21. with open('reddit.csv', 'w', newline='') as csvfile:
  22. writer = csv.writer(csvfile, delimiter=',')
  23. writer.writerow(criptoclean)
  24.  
  25. URL = "https://apewisdom.io/all-crypto/"
  26. page = requests.get(URL)
  27. soup = BeautifulSoup(page.content, "html.parser")
  28. criptos2 = soup.find_all("td", class_="td-center rh-sm")
  29. print("######################")
  30. #print(str(criptos2))
  31. criptos2 = str(criptos2)
  32. criptos2 = criptos2.replace('<td class="td-center rh-sm" data-sort="','|')
  33. criptos2 = criptos2.replace('</td>,','|')
  34. criptos2 = criptos2.replace(' ','')
  35. criptos2 = criptos2.replace('[','')
  36. criptos2 = criptos2.replace('</span></td>]','')
  37. criptos2 = criptos2.replace('<spanclass="percentage-red">','')
  38. criptos2 = criptos2.replace('<spanclass="percentage-green">','')
  39. criptos2 = criptos2.replace("N/A","0%")
  40. criptos2 = criptos2.replace('</span></td>','')
  41. criptos2 = criptos2.replace('</span>','')
  42. criptoclean2 = criptos2.split("|")
  43. #remove empty data from the list
  44. criptoclean2[:] = [x for x in criptoclean2 if x]
  45. #extract only non par
  46. criptoclean2 = [criptoclean2[i] for i in range(len(criptoclean2)) if i % 2 == 1]
  47. print("#############")
  48. #extract only data with percentages and clean the dirty data
  49. pattern = r'(-?\d+%)+'
  50. percentages = [re.search(pattern, elem).group(1) for elem in criptoclean2]
  51. print(percentages)
  52.  
  53. with open('redditpercentages.csv', 'w+', newline='') as csvfile:
  54. writer = csv.writer(csvfile, delimiter=',')
  55. writer.writerow(percentages)
  56.  
  57. #########################
  58. #Merge de data:
  59. datamerge = {
  60. "Date": str(date),
  61. "Ranking1": str(criptoclean[0]),
  62. "Ranking2": str(criptoclean[1]),
  63. "Ranking3": str(criptoclean[2]),
  64. "Ranking1percentage": str(percentages[0]),
  65. "Ranking2percentage": str(percentages[1]),
  66. "Ranking3percentage": str(percentages[2])
  67. }
  68. print("##########")
  69. print(datamerge)
  70.  
  71. ######MAKING THE CSV
  72. with open('RedditData.csv', mode='a', newline='') as file:
  73. #
  74. writer = csv.DictWriter(file, fieldnames=['Date','Ranking1','Ranking2','Ranking3','Ranking1percentage','Ranking2percentage','Ranking3percentage'])
  75. # writing the index header
  76. if file.tell() == 0:
  77. writer.writeheader()
  78. # writing de data
  79. writer.writerow(datamerge)
Advertisement
Add Comment
Please, Sign In to add comment