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.

270 lines
11 KiB

1 year ago
  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. #parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  8. #stores info it needs in different lists, these lists are returned after being organized
  9. #@param: soup object looking at html page of description page
  10. #return: 'row' that contains a variety of lists that each hold info on the description page
  11. def bestcardingworld_description_parser(soup):
  12. # Fields to be parsed
  13. topic = "-1" # 0 topic name
  14. user = [] # 1 all users of each post
  15. addDate = [] # 2 all dated of each post
  16. feedback = [] # 3 all feedbacks of each user (this was found in just one Forum and with a number format)
  17. status = [] # 4 all user's authority in each post such as (adm, member, dangerous)
  18. reputation = [] # 5 all users's karma in each post (usually found as a number)
  19. sign = [] # 6 all user's signature in each post (usually a standard message after the content of the post)
  20. post = [] # 7 all messages of each post
  21. interest = [] # 8 all user's interest in each post
  22. # Finding the topic (should be just one coming from the Listing Page)
  23. li = soup.find("h2", {"class": "topic-title"})
  24. topic = li.text
  25. topic = topic.replace(",","")
  26. topic = topic.replace("\n","")
  27. topic = cleanString(topic.strip())
  28. # Finding the repeated tag that corresponds to the listing of posts
  29. # posts = soup.find("form", {"name": "quickModForm"}).findAll('div', {"class": "windowbg"}) + \
  30. # soup.find("form", {"name": "quickModForm"}).findAll('div', {"class": "windowbg2"})
  31. posts = soup.findAll('div', {"class": "post has-profile bg2"}) + \
  32. soup.findAll('div', {"class": "post has-profile bg1"})
  33. # For each message (post), get all the fields we are interested to:
  34. for ipost in posts:
  35. # Finding a first level of the HTML page
  36. #post_wrapper = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "poster"})
  37. post_wrapper = ipost.find('a', {"class": "username-coloured"})
  38. # Finding the author (user) of the post
  39. #author = post_wrapper.find('h4')
  40. author = post_wrapper.text.strip()
  41. user.append(cleanString(author)) # Remember to clean the problematic characters
  42. # Finding the status of the author
  43. smalltext = ipost.find('dl', {"class": "postprofile"})
  44. # Testing here two possibilities to find this status and combine them
  45. # BestCarding does not have membergroup and postgroup
  46. membergroup = smalltext.find('li', {"class": "membergroup"})
  47. postgroup = smalltext.find('li', {"class": "postgroup"})
  48. if membergroup != None:
  49. membergroup = membergroup.text.strip()
  50. if postgroup != None:
  51. postgroup = postgroup.text.strip()
  52. membergroup = membergroup + " - " + postgroup
  53. else:
  54. if postgroup != None:
  55. membergroup = postgroup.text.strip()
  56. else:
  57. membergroup = "-1"
  58. status.append(cleanString(membergroup))
  59. # Finding the interest of the author
  60. # BestCarding does not have blurb
  61. blurb = smalltext.find('li', {"class": "blurb"})
  62. if blurb != None:
  63. blurb = blurb.text.strip()
  64. else:
  65. blurb = "-1"
  66. interest.append(cleanString(blurb))
  67. # Finding the reputation of the user
  68. # BestCarding does not have karma
  69. karma = smalltext.find('li', {"class": "karma"})
  70. if karma != None:
  71. karma = karma.text
  72. karma = karma.replace("Community Rating: ","")
  73. karma = karma.replace("Karma: ","")
  74. karma = karma.strip()
  75. else:
  76. karma = "-1"
  77. reputation.append(cleanString(karma))
  78. # Getting here another good tag to find the post date, post content and users' signature
  79. postarea = ipost.find('div', {"class": "inner"})
  80. dt = ipost.find('p', {"class": "author"}).text.split('»')[1]
  81. #dt = dt.strip().split()
  82. dt = dt.strip()
  83. date_time_obj = datetime.strptime(dt, '%a %b %d, %Y %I:%M %p')
  84. stime = date_time_obj.strftime('%a %b %d, %Y')
  85. sdate = date_time_obj.strftime('%I:%M %p')
  86. addDate.append(date_time_obj)
  87. # Finding the date of the post
  88. # date_time_obj = datetime.strptime(dt, '%a %b %d, %Y %I:%M %p')
  89. # smalltext = postarea.find('div', {"class": "flow_hidden"}).find('div', {"class": "keyinfo"})\
  90. # .find('div', {"class": "smalltext"})
  91. # sdatetime = smalltext.text
  92. # sdatetime = sdatetime.replace(u"\xab","") # Removing unnecessary characters
  93. # sdatetime = sdatetime.replace(u"\xbb","") # Removing unnecessary characters
  94. # sdatetime = sdatetime.split("on: ") # Removing unnecessary characters
  95. # sdatetime = sdatetime[1].strip()
  96. # stime = sdatetime[:-12:-1] # Finding the time of the post
  97. # stime = stime[::-1]
  98. # sdate = sdatetime.replace(stime,"") # Finding the date of the post
  99. # sdate = sdate.replace(",","")
  100. # sdate = sdate.strip()
  101. # Covert the date of the post that can be informed as: "12 February 2016", "today", "yesterday". We need
  102. # a date format here as "mm/dd/yyyy"
  103. #addDate.append(convertDate(sdate,"english", crawlerDate) + " " + stime)
  104. # Finding the post
  105. inner = postarea.find('div', {"class": "content"})
  106. inner = inner.text.strip()
  107. post.append(cleanString(inner))
  108. # Finding the users's signature
  109. #signature = ipost.find('div', {"class": "post_wrapper"}).find('div', {"class": "moderatorbar"}).find('div', {"class": "signature"})
  110. signature = ipost.find('div', {"class": "post_wrapper"})
  111. if signature != None:
  112. signature = signature.text.strip()
  113. else:
  114. signature = "-1"
  115. sign.append(cleanString(signature))
  116. # As no information about users's feedback was found, just assign "-1" to the variable
  117. feedback.append("-1")
  118. # Populate the final variable (this should be a list with all fields scraped)
  119. row = (topic, post, user, addDate, feedback, status, reputation, sign, interest)
  120. # Sending the results
  121. return row
  122. # This is the method to parse the Listing Pages (one page with many posts)
  123. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  124. #stores info it needs in different lists, these lists are returned after being organized
  125. #@param: soup object looking at html page of listing page
  126. #return: 'row' that contains a variety of lists that each hold info on the listing page
  127. def bestcardingworld_listing_parser(soup):
  128. nm = 0 # this variable should receive the number of topics
  129. topic = [] # 1 all topics
  130. board = "-1" # 2 board name (the previous level of the topic in the Forum categorization tree.
  131. # For instance: Security/Malware/Tools to hack Facebook. The board here should be Malware)
  132. view = [] # 3 number of views of each topic
  133. post = [] # 4 number of posts of each topic
  134. user = [] # 5 all users of each topic
  135. addDate = [] # 6 when the topic was created (difficult to find)
  136. href = [] # 16 this variable should receive all cleaned urls (we will use this to do the marge between Listing and Description pages)
  137. # Finding the board (should be just one)
  138. parents = soup.find('ul', {"class": "linklist navlinks"}).findAll('a')
  139. board = parents[1].text + u"->" + parents[2].text
  140. board = board.replace(u"\xbb","")
  141. board = cleanString(board.strip())
  142. # Finding the repeated tag that corresponds to the listing of topics
  143. itopics = soup.find('ul', {"class": "topiclist topics"}).findAll('div',{"class": "list-inner"})
  144. replies = soup.find('ul', {"class": "topiclist topics"}).findAll('dd',{"class": "posts"})
  145. views = soup.find('ul', {"class": "topiclist topics"}).findAll('dd',{"class": "views"})
  146. index = 0
  147. for itopic in itopics:
  148. # For each topic found, the structure to get the rest of the information can be of two types. Testing all of them
  149. # to don't miss any topic
  150. # tds = itopic.findAll('td', {"class": "subject stickybg2"})
  151. #
  152. # if len(tds) > 0:
  153. # tag.append("strong")
  154. # tag.append("subject stickybg2")
  155. # tag.append("stats stickybg")
  156. # else:
  157. # tds = itopic.findAll('td', {"class": "subject windowbg2"})
  158. # if len(tds) > 0:
  159. # tag.append("span")
  160. # tag.append("subject windowbg2")
  161. # tag.append("stats windowbg")
  162. # Adding the topic to the topic list
  163. topics = itopic.find('a', {"class": "topictitle"}).text
  164. topic.append(cleanString(topics))
  165. # Counting how many topics we have found so far
  166. nm = len(topic)
  167. # Adding the url to the list of urls
  168. link = itopic.find('a', {"class": "topictitle"}).get('href')
  169. link = cleanLink(link)
  170. href.append(link)
  171. # Finding the author of the topic
  172. ps = itopic.find('div', {"class":"responsive-hide"}).find('a', {"class": "username-coloured"}).text
  173. author = ps.strip()
  174. user.append(cleanString(author))
  175. # Finding the number of replies
  176. posts = replies[index].text.split()[0]
  177. posts = posts.strip()
  178. post.append(cleanString(posts))
  179. # Finding the number of Views
  180. tview = views[index].text.split()[0]
  181. tview = tview.strip()
  182. view.append(cleanString(tview))
  183. # If no information about when the topic was added, just assign "-1" to the variable
  184. #CryptBB doesn't show when topic was first posted on listing page
  185. dt = itopic.find('div', {"class": "responsive-hide"}).text.split('»')[1]
  186. dt = dt.strip()
  187. date_time_obj = datetime.strptime(dt,'%a %b %d, %Y %I:%M %p')
  188. addDate.append(date_time_obj)
  189. #addDate.append("-1")
  190. index += 1
  191. return organizeTopics("BestCardingWorld", nm, topic, board, view, post, user, addDate, href)
  192. #called by the crawler to get description links on a listing page
  193. #@param: beautifulsoup object that is using the correct html page (listing page)
  194. #return: list of description links from a listing page
  195. def bestcardingworld_links_parser(soup):
  196. # Returning all links that should be visited by the Crawler
  197. href = []
  198. listing = soup.find('div', {"class": "forumbg"}).find('ul', {"class": "topiclist topics"}).findAll('li', {"class": "row bg1"}) + \
  199. soup.find('div', {"class": "forumbg"}).find('ul', {"class": "topiclist topics"}).findAll('li', {"class": "row bg2"})
  200. for a in listing:
  201. bae = a.find('a', {"class": "topictitle"}, href=True)
  202. link = bae['href']
  203. href.append(link)
  204. return href