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.

188 lines
7.1 KiB

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 MarketPlaces.Utilities.utilities import *
  4. # Here, we are importing BeautifulSoup to search through the HTML tree
  5. from bs4 import BeautifulSoup
  6. #parses description pages, so takes html pages of description pages using soup object, and parses it for info it needs
  7. #stores info it needs in different lists, these lists are returned after being organized
  8. #@param: soup object looking at html page of description page
  9. #return: 'row' that contains a variety of lists that each hold info on the description page
  10. def torbay_description_parser(soup):
  11. # Fields to be parsed
  12. vendor = "-1" # 0 *Vendor_Name
  13. success = "-1" # 1 Vendor_Successful_Transactions
  14. rating_vendor = "-1" # 2 Vendor_Rating
  15. name = "-1" # 3 *Product_Name
  16. describe = "-1" # 4 Product_Description
  17. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about that much
  18. MS = "-1" # 6 Product_MS_Classification (Microsoft Security) dont worry about that much
  19. category = "-1" # 7 Product_Category
  20. views = "-1" # 8 Product_Number_Of_Views
  21. reviews = "-1" # 9 Product_Number_Of_Reviews
  22. rating_item = "-1" # 10 Product_Rating
  23. addDate = "-1" # 11 Product_AddedDate
  24. BTC = "-1" # 12 Product_BTC_SellingPrice
  25. USD = "-1" # 13 Product_USD_SellingPrice
  26. EURO = "-1" # 14 Product_EURO_SellingPrice
  27. sold = "-1" # 15 Product_QuantitySold
  28. left = "-1" # 16 Product_QuantityLeft
  29. shipFrom = "-1" # 17 Product_ShippedFrom
  30. shipTo = "-1" # 18 Product_ShippedTo
  31. # Finding Product Name
  32. try:
  33. product_name = soup.find('div', {'class': 'product-information'}).find('h1').text
  34. name = cleanString(product_name.strip())
  35. except:
  36. try:
  37. product_name = soup.find('div', {'class': 'profile-info'}).find('h2').text
  38. name = cleanString(product_name.strip())
  39. except:
  40. # print(e)
  41. print("product name")
  42. # Finding Vendor FIx
  43. try:
  44. vendor_name = soup.find('div', {"class": "profile-info"}).find('h2').text
  45. vendor = cleanString(vendor_name.strip())
  46. except:
  47. print("description vendor name failed\n")
  48. # Finding Prices
  49. try:
  50. USD = soup.find('div', {'class': "total-price"}).find('span').text.strip()
  51. except:
  52. print("description price failed\n")
  53. # Finding the Product Category
  54. try:
  55. cat = soup.find('div', {'class': "profile-info"}).find('p').text
  56. category = cleanString(cat.strip())
  57. except:
  58. print("description product category failed")
  59. # Finding the Product description
  60. try:
  61. describe = soup.find('div', {'class': "info"}).find('p').text
  62. if "\n" in describe:
  63. describe = describe.replace("\n", " ")
  64. describe = describe.replace("\r", " ")
  65. describe = cleanString(describe.strip())
  66. except:
  67. # print("product desc")
  68. try:
  69. describe = soup.find('div', {'class': 'info'}).text
  70. describe = cleanString(describe.strip())
  71. except:
  72. print("Product description")
  73. # Populating the final variable (this should be a list with all fields scraped)
  74. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  75. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  76. # Sending the results
  77. return row
  78. #parses listing pages, so takes html pages of listing pages using soup object, and parses it for info it needs
  79. #stores info it needs in different lists, these lists are returned after being organized
  80. #@param: soup object looking at html page of listing page
  81. #return: 'row' that contains a variety of lists that each hold info on the listing page
  82. def torbay_listing_parser(soup):
  83. # Fields to be parsed
  84. nm = 0 # *Total_Products (Should be Integer)
  85. mktName = "TorBay" # 0 *Marketplace_Name
  86. vendor = [] # 1 *Vendor y
  87. rating_vendor = [] # 2 Vendor_Rating
  88. success = [] # 3 Vendor_Successful_Transactions
  89. name = [] # 4 *Product_Name y
  90. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures) dont worry about this
  91. MS = [] # 6 Product_MS_Classification (Microsoft Security) dont worry about this
  92. category = [] # 7 Product_Category y
  93. describe = [] # 8 Product_Description
  94. views = [] # 9 Product_Number_Of_Views
  95. reviews = [] # 10 Product_Number_Of_Reviews
  96. rating_item = [] # 11 Product_Rating
  97. addDate = [] # 12 Product_AddDate
  98. BTC = [] # 13 Product_BTC_SellingPrice
  99. USD = [] # 14 Product_USD_SellingPrice y
  100. EURO = [] # 15 Product_EURO_SellingPrice
  101. sold = [] # 16 Product_QuantitySold
  102. qLeft = [] # 17 Product_QuantityLeft
  103. shipFrom = [] # 18 Product_ShippedFrom
  104. shipTo = [] # 19 Product_ShippedTo
  105. href = [] # 20 Product_Links
  106. listing = soup.findAll('div', {"class": "product-card"})
  107. # Populating the Number of Products
  108. nm = len(listing)
  109. for a in listing:
  110. try:
  111. product_name = a.find('p', {'class': 'name'}).text
  112. name.append(cleanString(product_name.strip()))
  113. except:
  114. print("product name")
  115. try:
  116. prod = a.find('p', {'class': 'price'}).text # price
  117. USD.append(cleanString(prod.strip()))
  118. except:
  119. print("USD")
  120. try:
  121. ven = a.find('div', {'class': 'pc-footer'}).find('div').find('a').text # pc-footer
  122. vendor.append(cleanString(ven.strip()))
  123. # print(ven)
  124. except:
  125. print("vendor")
  126. try:
  127. h = a.find('p', {'class': 'name'}).find('a').get('href')
  128. href.append(h)
  129. except:
  130. print("in href")
  131. CVE.append("-1")
  132. MS.append("-1")
  133. rating_vendor.append("-1")
  134. success.append("-1")
  135. describe.append("-1")
  136. views.append("-1")
  137. reviews.append("-1")
  138. rating_item.append("-1")
  139. addDate.append("-1")
  140. BTC.append("-1")
  141. EURO.append("-1")
  142. sold.append("-1")
  143. qLeft.append("-1")
  144. shipFrom.append("-1")
  145. shipTo.append("-1")
  146. category.append("Hacking")
  147. # Populate the final variable (this should be a list with all fields scraped)
  148. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  149. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  150. #called by the crawler to get description links on a listing page
  151. #@param: beautifulsoup object that is using the correct html page (listing page)
  152. #return: list of description links from a listing page
  153. def torbay_links_parser(soup):
  154. # Returning all links that should be visited by the Crawler
  155. href = []
  156. listing = soup.find('section', {"id": "content"}).findAll('div', {"class": "product-card"})
  157. for a in listing:
  158. bae = a.find('div', {"class": "pc-footer"}).find('a', {"class": "btn btn-primary"}, href=True)
  159. link = bae['href']
  160. href.append(link)
  161. return href