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.

187 lines
7.6 KiB

  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. # This is the method to parse the Description Pages (one page to each Product in the Listing Pages)
  7. def kingdom_description_parser(soup):
  8. # Fields to be parsed
  9. vendor = "-1" # 0 *Vendor_Name
  10. success = "-1" # 1 Vendor_Successful_Transactions
  11. rating_vendor = "-1" # 2 Vendor_Rating
  12. name = "-1" # 3 *Product_Name
  13. describe = "-1" # 4 Product_Description
  14. CVE = "-1" # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  15. MS = "-1" # 6 Product_MS_Classification (Microsoft Security)
  16. category = "-1" # 7 Product_Category
  17. views = "-1" # 8 Product_Number_Of_Views
  18. reviews = "-1" # 9 Product_Number_Of_Reviews
  19. rating_item = "-1" # 10 Product_Rating
  20. addDate = "-1" # 11 Product_AddedDate
  21. BTC = "-1" # 12 Product_BTC_SellingPrice
  22. USD = "-1" # 13 Product_USD_SellingPrice
  23. EURO = "-1" # 14 Product_EURO_SellingPrice
  24. sold = "-1" # 15 Product_QuantitySold
  25. left = "-1" # 16 Product_QuantityLeft
  26. shipFrom = "-1" # 17 Product_ShippedFrom
  27. shipTo = "-1" # 18 Product_ShippedTo
  28. # Finding Product Name
  29. tag = soup.find('div', {"class": "col-md-9"})
  30. desc = tag.find('div',{"class": "col-md-8"}).find('div', {"class": "box-cont"})
  31. name = tag.find('div',{"class": "col-md-8"}).find('div', {"class": "box-head"}).text
  32. name = name.replace('\n', ' ')
  33. name = name.replace(',', ' ')
  34. name = name.strip()
  35. # Finding Prices
  36. # Kingdom prices can be shown in a variety of currencies, not all in USD, so keeping currency
  37. rows = desc.find_all('div', {"class", "row"}, recursive=False)
  38. price = rows[-1].find('div', {"class": "row"}).find('h3').text
  39. price = price.replace(',', '')
  40. price = price.strip()
  41. # USD = price.replace("USD",'')
  42. BTC = rows[-1].find('div', {"class": "row"}).find_next_sibling('div').find('span').text
  43. # Finding Vendor
  44. vendor = rows[0].select_one('a[href^="/user"]').text
  45. vendor = vendor.replace(",", " ")
  46. vendor = vendor.strip()
  47. # Finding Shipment Information (Origem)
  48. descs = rows[0].find_all('div', {"class": "col-md-3 text-right"})
  49. shipFrom = descs[2].text
  50. shipFrom = shipFrom.replace(",", "")
  51. shipFrom = shipFrom.strip()
  52. # Finding Shipment Information (Destiny)
  53. shipTo = rows[-1].find('div', {"class": "col-md-6"}).text
  54. shipTo = shipTo.replace("Ship to:","")
  55. shipTo = shipTo.replace(",","").strip()
  56. if(shipTo == ''):
  57. shipTo = -1
  58. # Finding the Product Category
  59. category = descs[0].text
  60. category = category.replace(",", "")
  61. category = category.strip()
  62. # Finding the Product Quantity Available
  63. left = descs[1].text
  64. left = left.replace(",", "")
  65. left = left.strip()
  66. # Finding when the Product was Added
  67. dt = descs[-1].text.strip()
  68. addDate = datetime.strptime(dt, '%d.%m.%Y')
  69. # Finding the Product description
  70. describe = cleanString(soup.find('div', {"id": "descriptionContent"}).text)
  71. # Finding the Number of Product Reviews
  72. review = len(soup.find('div', {"id": "feedbackContent"}).find_all(recursive=False))
  73. # Searching for CVE and MS categories
  74. # no cve or ms in Kingdom
  75. # Populating the final variable (this should be a list with all fields scraped)
  76. row = (vendor, rating_vendor, success, name, describe, CVE, MS, category, views, reviews, rating_item, addDate,
  77. BTC, USD, EURO, sold, left, shipFrom, shipTo)
  78. # Sending the results
  79. return row
  80. def kingdom_listing_parser(soup):
  81. # Fields to be parsed
  82. nm = 0 # *Total_Products (Should be Integer)
  83. mktName = "Kingdom" # 0 *Marketplace_Name
  84. vendor = [] # 1 *Vendor y
  85. rating_vendor = [] # 2 Vendor_Rating
  86. success = [] # 3 Vendor_Successful_Transactions
  87. name = [] # 4 *Product_Name y
  88. CVE = [] # 5 Product_CVE_Classification (Common Vulnerabilities and Exposures)
  89. MS = [] # 6 Product_MS_Classification (Microsoft Security)
  90. category = [] # 7 Product_Category y
  91. describe = [] # 8 Product_Description
  92. views = [] # 9 Product_Number_Of_Views
  93. reviews = [] # 10 Product_Number_Of_Reviews
  94. rating_item = [] # 11 Product_Rating
  95. addDate = [] # 12 Product_AddDate
  96. BTC = [] # 13 Product_BTC_SellingPrice
  97. USD = [] # 14 Product_USD_SellingPrice y
  98. EURO = [] # 15 Product_EURO_SellingPrice
  99. sold = [] # 16 Product_QuantitySold
  100. qLeft =[] # 17 Product_QuantityLeft
  101. shipFrom = [] # 18 Product_ShippedFrom
  102. shipTo = [] # 19 Product_ShippedTo
  103. href = [] # 20 Product_Links
  104. listing = soup.find('div', {"id": "p0"}).find('div').find_all('div', {"class": "row"}, recursive=False)
  105. # Populating the Number of Products
  106. nm = len(listing)
  107. for a in listing:
  108. # Finding Prices
  109. #in array USD, there may be prices not in USD, so includes currency as well
  110. prices = a.find('div', {"class": "col-md-3"})
  111. u = prices.find('h3').text
  112. u = u.strip()
  113. u = u.replace(',', '')
  114. u = u.strip()
  115. USD.append(u)
  116. bc = prices.find('div').find('span').text
  117. BTC.append(bc)
  118. # Finding the Product
  119. product = a.find('div', {"class": "col-md-7"}).select_one('a[href^="/offer/view?"]').text
  120. product = product.replace('\n', ' ')
  121. product = product.replace(","," ")
  122. product = product.strip()
  123. name.append(product)
  124. # Finding the Vendor
  125. vendor_name = a.select_one('a[href^="/user"]').text
  126. vendor_name = vendor_name.replace(",", " ").replace('/', '')
  127. vendor_name = vendor_name.strip()
  128. vendor.append(vendor_name)
  129. # Adding the url to the list of urls
  130. link = a.find('div', {"class": "col-md-7"}).select_one('a[href^="/offer/view?"]')['href']
  131. link = cleanLink(link)
  132. href.append(link)
  133. # Searching for CVE and MS categories
  134. # cve and ms not in kingdom
  135. # Populate the final variable (this should be a list with all fields scraped)
  136. return organizeProducts(mktName, nm, vendor, rating_vendor, success, name, CVE, MS, category, describe, views,
  137. reviews, rating_item, addDate, BTC, USD, EURO, sold, qLeft, shipFrom, shipTo, href)
  138. def kingdom_links_parser(soup):
  139. # Returning all links that should be visited by the Crawler
  140. href = []
  141. listing = soup.findAll('div', {"class": "col-md-7"})
  142. for a in listing:
  143. link = a.select_one('a[href^="/offer/view?"]')
  144. link = link['href']
  145. href.append(link)
  146. return href