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.

247 lines
9.2 KiB

  1. __author__ = 'DarkWeb'
  2. # Here, we are importing the auxiliary functions to clean or convert data
  3. from Forums.Utilities.utilities import *
  4. # Here, we are importing BeautifulSoup to search through the HTML tree
  5. from bs4 import BeautifulSoup
  6. # This is the method to parse the Description Pages (one page to each topic in the Listing Pages)
  7. def helium_description_parser(soup):
  8. # Fields to be parsed
  9. topic = "-1" # topic name
  10. user = [] # all users of each post
  11. addDate = [] # all dated of each post
  12. feedback = [] # all feedbacks of each vendor (this was found in just one Forum and with a number format)
  13. status = [] # all user's authority in each post such as (adm, member, dangerous)
  14. reputation = [] # all users's karma in each post (usually found as a number)
  15. sign = [] # all user's signature in each post (usually a standard message after the content of the post)
  16. post = [] # all messages of each post
  17. interest = [] # all user's interest in each post
  18. # Finding the topic (should be just one coming from the Listing Page)
  19. li = soup.find("h4", {"class": "text-truncated"})
  20. topic = li.text
  21. topic = topic.replace("Topic:", "")
  22. topic = topic.replace("Post Reply", "")
  23. topic = topic.replace(",", "")
  24. topic = topic.replace("\n", "")
  25. topic = cleanString(topic.strip())
  26. # Finding the repeated tag that corresponds to the listing of posts
  27. posts = soup.findAll('div', {"id": "a9"})
  28. # For each message (post), get all the fields we are interested to:
  29. for ipost in posts:
  30. # Finding a first level of the HTML page
  31. # Finding the author (user) of the post
  32. heading = ipost.find('div', {"class": "panel-heading"})
  33. title = heading.find('div', {"class": "panel-title"}).text
  34. author = title.replace("User:", "")
  35. author = author.strip()
  36. user.append(cleanString(author)) # Remember to clean the problematic characters
  37. # Finding the status of the author
  38. # Testing here two possibilities to find this status and combine them
  39. # Helium does not have membergroup and postgroup
  40. membergroup = heading.find('li', {"class": "membergroup"})
  41. postgroup = heading.find('li', {"class": "postgroup"})
  42. if membergroup != None:
  43. membergroup = membergroup.text.strip()
  44. if postgroup != None:
  45. postgroup = postgroup.text.strip()
  46. membergroup = membergroup + " - " + postgroup
  47. else:
  48. if postgroup != None:
  49. membergroup = postgroup.text.strip()
  50. else:
  51. membergroup = "-1"
  52. status.append(cleanString(membergroup))
  53. # Finding the interest of the author
  54. # Helium does not have blurb
  55. blurb = heading.find('li', {"class": "blurb"})
  56. if blurb != None:
  57. blurb = blurb.text.strip()
  58. else:
  59. blurb = "-1"
  60. interest.append(cleanString(blurb))
  61. # Finding the reputation of the user
  62. # Helium does not have karma
  63. karma = heading.find('li', {"class": "karma"})
  64. if karma != None:
  65. karma = karma.text
  66. karma = karma.replace("Community Rating: ","")
  67. karma = karma.replace("Karma: ","")
  68. karma = karma.strip()
  69. else:
  70. karma = "-1"
  71. reputation.append(cleanString(karma))
  72. # Getting here another good tag to find the post date, post content and users' signature
  73. postarea = ipost.find('div', {"class": "content_body"})
  74. # Finding the date of the post
  75. # Helium does not have date
  76. addDate.append("-1")
  77. # dt = ipost.find('p', {"class": "author"}).text.split('»')[1]
  78. # # dt = dt.strip().split()
  79. # dt = dt.strip()
  80. # date_time_obj = datetime.strptime(dt, '%a %b %d, %Y %I:%M %p')
  81. # stime = date_time_obj.strftime('%a %b %d, %Y')
  82. # sdate = date_time_obj.strftime('%I:%M %p')
  83. # addDate.append(date_time_obj)
  84. # date_time_obj = datetime.strptime(dt, '%a %b %d, %Y %I:%M %p')
  85. # smalltext = postarea.find('div', {"class": "flow_hidden"}).find('div', {"class": "keyinfo"})\
  86. # .find('div', {"class": "smalltext"})
  87. # sdatetime = smalltext.text
  88. # sdatetime = sdatetime.replace(u"\xab","") # Removing unnecessary characters
  89. # sdatetime = sdatetime.replace(u"\xbb","") # Removing unnecessary characters
  90. # sdatetime = sdatetime.split("on: ") # Removing unnecessary characters
  91. # sdatetime = sdatetime[1].strip()
  92. # stime = sdatetime[:-12:-1] # Finding the time of the post
  93. # stime = stime[::-1]
  94. # sdate = sdatetime.replace(stime,"") # Finding the date of the post
  95. # sdate = sdate.replace(",","")
  96. # sdate = sdate.strip()
  97. # Covert the date of the post that can be informed as: "12 February 2016", "today", "yesterday". We need
  98. # a date format here as "mm/dd/yyyy"
  99. #addDate.append(convertDate(sdate,"english", crawlerDate) + " " + stime)
  100. # Finding the post
  101. paragraphs = postarea.find_all('p')
  102. p = ""
  103. for paragraph in paragraphs:
  104. p += paragraph.text.strip() + " "
  105. quote = postarea.find('div', {"class": "standard_quote"})
  106. if quote != None:
  107. q = quote.text.strip()
  108. p.replace(q, "")
  109. post.append(cleanString(p.strip()))
  110. # Finding the users's signature
  111. # Helium does not have signature
  112. #signature = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "moderatorbar"}).find('div', {"class": "signature"})
  113. signature = ipost.find('div', {"class": "post_wrapper"})
  114. if signature != None:
  115. signature = signature.text.strip()
  116. else:
  117. signature = "-1"
  118. sign.append(cleanString(signature))
  119. # As no information about users's feedback was found, just assign "-1" to the variable
  120. feedback.append("-1")
  121. # Populate the final variable (this should be a list with all fields scraped)
  122. row = (topic, post, user, addDate, feedback, status, reputation, sign, interest)
  123. # Sending the results
  124. return row
  125. # This is the method to parse the Listing Pages (one page with many posts)
  126. def helium_listing_parser(soup):
  127. board = "-1" # board name (the previous level of the topic in the Forum categorization tree.
  128. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  129. nm = 0 # this variable should receive the number of topics
  130. topic = [] # all topics
  131. user = [] # all users of each topic
  132. post = [] # number of posts of each topic
  133. view = [] # number of views of each topic
  134. addDate = [] # when the topic was created (difficult to find)
  135. href = [] # this variable should receive all cleaned urls (we will use this to do the marge between
  136. # Listing and Description pages)
  137. # Finding the board (should be just one)
  138. parents = soup.find('div', {"class": "col-md-12"}).findAll('li')
  139. board = parents[1].text + u"->" + parents[2].get('title')
  140. board = board.replace("\n", "")
  141. board = cleanString(board.strip())
  142. # Finding the repeated tag that corresponds to the listing of topics
  143. itopics = soup.find('table', {"class": "table"}).find('tbody').findAll('td', {"class": "col-md-8"})
  144. repliesViews = soup.find('table', {"class": "table"}).find('tbody').findAll('td', {"class": "col-md-2"})
  145. # Counting how many topics we have found so far
  146. nm = len(itopics)
  147. index = 0
  148. for itopic in itopics:
  149. # Adding the topic to the topic list
  150. topics = itopic.find('a').get('title')
  151. topics = topics.replace(",", "")
  152. topic.append(cleanString(topics.strip()))
  153. # Adding the url to the list of urls
  154. link = itopic.find('a').get('href')
  155. link = cleanLink(link)
  156. href.append(link)
  157. # Finding the author of the topic
  158. author = itopic.find('strong').text
  159. user.append(cleanString(author.strip()))
  160. rv = repliesViews[index].find('p').text.split()
  161. # Finding the number of replies
  162. posts = rv[0].replace("Replies", "")
  163. post.append(cleanString(posts.strip()))
  164. # Finding the number of Views
  165. tview = rv[1].replace("Views", "")
  166. view.append(cleanString(tview.strip()))
  167. # If no information about when the topic was added, just assign "-1" to the variable
  168. # dt = itopic.find('div', {"class": "responsive-hide"}).text.split('»')[1]
  169. # dt = dt.strip()
  170. # date_time_obj = datetime.strptime(dt,'%a %b %d, %Y %I:%M %p')
  171. # addDate.append(date_time_obj)
  172. addDate.append("-1")
  173. index += 1
  174. return organizeTopics("Helium", nm, topic, board, view, post, user, addDate, href)
  175. def helium_links_parser(soup):
  176. # Returning all links that should be visited by the Crawler
  177. href = []
  178. listing = soup.find('table', {"class": "table"}).find('tbody').findAll('td', {"class": "col-md-8"})
  179. for a in listing:
  180. bae = a.find('a', href=True)
  181. link = bae['href']
  182. href.append(link)
  183. return href