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.

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