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.

277 lines
11 KiB

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