this is based on calsyslab project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.6 KiB

  1. __author__ = 'Helium'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from typing import List
  4. from Forums.Utilities.utilities import *
  5. from datetime import date
  6. from datetime import timedelta
  7. import re
  8. # Here, we are importing BeautifulSoup to search through the HTML tree
  9. from bs4 import BeautifulSoup, ResultSet, Tag
  10. # This is the method to parse the Description Pages (one page to each topic in the Listing Pages)
  11. def HiddenAnswers_description_parser(soup: BeautifulSoup):
  12. topic: str = "-1" # topic name
  13. user: List[str] = [] # all users of each post
  14. addDate: List[datetime] = [] # all dated of each post
  15. feedback: List[str] = [] # all feedbacks of each vendor (this was found in just one Forum and with a number format)
  16. status: List[str] = [] # all user's authority in each post such as (adm, member, dangerous)
  17. reputation: List[str] = [] # all user's karma in each post (usually found as a number)
  18. sign: List[str] = [] # all user's signature in each post (usually a standard message after the content of the post)
  19. post: List[str] = [] # all messages of each post
  20. interest: List[str] = [] # all user's interest in each post
  21. image = []
  22. image_user = []
  23. # Finding the topic (should be just one coming from the Listing Page)
  24. li = soup.find("h1").find("span", {"itemprop": "name"})
  25. topic = li.text
  26. question: Tag = soup.find("div", {"class": "qa-part-q-view"})
  27. question_user = question.find("span", {"class": "qa-q-view-who-data"}).text
  28. user.append(cleanString(question_user.strip()))
  29. question_time = question.find("span", {"class": "qa-q-view-when-data"}).find("time").get("datetime")
  30. datetime_string = question_time.split("+")[0]
  31. datetime_obj = datetime.strptime(datetime_string, "%Y-%m-%dT%H:%M:%S")
  32. addDate.append(datetime_obj)
  33. question_user_status = question.find("span", {"class": "qa-q-view-who-title"}).text
  34. status.append(cleanString(question_user_status.strip()))
  35. question_user_karma = question.find("span", {"class": "qa-q-view-who-points-data"}).text
  36. # Convert karma to pure numerical string
  37. if question_user_karma.find("k") > -1:
  38. question_user_karma = str(float(question_user_karma.replace("k", "")) * 1000)
  39. reputation.append(cleanString(question_user_karma.strip()))
  40. question_content = question.find("div", {"class": "qa-q-view-content qa-post-content"}).text
  41. post.append(cleanString(question_content.strip()))
  42. feedback.append("-1")
  43. sign.append("-1")
  44. interest.append("-1")
  45. img = question.find('div', {"class": "qa-q-view-content qa-post-content"}).find('img')
  46. if img is not None:
  47. img = img.get('src').split('base64,')[-1]
  48. else:
  49. img = "-1"
  50. image.append(img)
  51. img = question.find('span', {"class": "qa-q-view-avatar-meta"}).find('img')
  52. if img is not None:
  53. img = img.get('src').split('base64,')[-1]
  54. else:
  55. img = "-1"
  56. image_user.append(img)
  57. answer_list: ResultSet[Tag] = soup.find("div", {"class": "qa-a-list"}).find_all("div", {"class": "qa-a-list-item"})
  58. for replies in answer_list:
  59. user_name = replies.find("span", {"class", "qa-a-item-who-data"}).text
  60. user.append(cleanString(user_name.strip()))
  61. date_added = replies.find("span", {"class": "qa-a-item-when"}).find("time", {"itemprop": "dateCreated"}).get('datetime')
  62. date_string = date_added.split("+")[0]
  63. datetime_obj = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S")
  64. addDate.append(datetime_obj)
  65. post_data = replies.find("div", {"class": "qa-a-item-content qa-post-content"}).find("div",{"itemprop":"text"}).text
  66. post.append(cleanString(post_data.strip()))
  67. user_reputations = replies.find("span", {"class", "qa-a-item-who-title"}).text
  68. status.append(cleanString(user_reputations.strip()))
  69. karma = replies.find("span", {"class": "qa-a-item-who-points-data"}).text
  70. # Convert karma to pure numerical string
  71. if karma.find("k") > -1:
  72. karma = str(float(karma.replace("k", "")) * 1000)
  73. reputation.append(cleanString(karma.strip()))
  74. feedback.append("-1")
  75. sign.append("-1")
  76. interest.append("-1")
  77. img = replies.find("div", {"class": "qa-a-item-content qa-post-content"}).find("div",{"itemprop":"text"}).find('img')
  78. if img is not None:
  79. img = img.get('src').split('base64,')[-1]
  80. else:
  81. img = "-1"
  82. image.append(img)
  83. img = replies.find('span', {"class": "qa-a-item-avatar-meta"}).find('img')
  84. if img is not None:
  85. img = img.get('src').split('base64,')[-1]
  86. else:
  87. img = "-1"
  88. image_user.append(img)
  89. # Populate the final variable (this should be a list with all fields scraped)
  90. row = (topic, user, status, reputation, interest, sign, post, feedback, addDate)
  91. # Sending the results
  92. return row
  93. def HiddenAnswers_listing_parser(soup: BeautifulSoup):
  94. board = "-1" # board name (the previous level of the topic in the Forum categorization tree.
  95. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  96. forum: str = "HiddenAnswers"
  97. nm: int = 0 # this variable should receive the number of topics
  98. topic: List[str] = [] # all topics
  99. user: List[str] = [] # all users of each topic
  100. post: List[int] = [] # number of posts of each topic
  101. view: List[int] = [] # number of views of each topic
  102. addDate: List[str] = [] # when the topic was created (difficult to find)
  103. href: List[str] = [] # this variable should receive all cleaned urls (we will use this to do the merge between
  104. # Listing and Description pages)
  105. # Finding the board
  106. literature = soup.find("div", {"class": "qa-main-heading"}).find("h1")
  107. board = literature.text
  108. queries_by_user: ResultSet[Tag] = soup.find("div", {"class": "qa-q-list"}).find_all("div", {"class": "qa-q-list-item"})
  109. for queries in queries_by_user:
  110. topic_of_query = queries.find("div", {"class": "qa-q-item-title"}).find("a").text
  111. topic.append(cleanString(topic_of_query.strip()))
  112. author = queries.find("span", {"class": "qa-q-item-who-data"}).find("a").text
  113. user.append(cleanString(author.strip()))
  114. num_answers = queries.find("span", {"class": "qa-a-count-data"}).text
  115. post.append(cleanString(num_answers.strip()))
  116. view.append("-1")
  117. date_posted = queries.find("span", {"class": "qa-q-item-when-data"}).text
  118. if date_posted.find("day") > 0:
  119. datetime_obj = datetime.now() - timedelta(days=1)
  120. else:
  121. try:
  122. datetime_obj = datetime.strptime(f"{date_posted} {date.today().year}", "%b %d %Y")
  123. except ValueError:
  124. datetime_obj = datetime.strptime(f"{date_posted}", "%b %d, %Y")
  125. addDate.append(datetime_obj)
  126. #this link will be cleaned
  127. listing_href = queries.find("div", {"class": "qa-q-item-title"}).find("a").get("href")
  128. href.append(listing_href)
  129. nm = len(topic)
  130. return organizeTopics(forum, nm, board, user, topic, view, post, href, addDate)
  131. #need to change this method
  132. def hiddenanswers_links_parser(soup):
  133. # Returning all links that should be visited by the Crawler
  134. href = []
  135. #print(soup.find('table', {"class": "tborder clear"}).find(
  136. # 'tbody').find_all('tr', {"class": "inline_row"}))
  137. listing = soup.find_all('div', {"class": "qa-q-item-title"})
  138. for a in listing:
  139. link = a.find('a').get('href')
  140. href.append(link)
  141. return href