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.

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